gpt4 book ai didi

javascript - 使用 sequelize 中的 where 条件根据距离进行过滤

转载 作者:行者123 更新时间:2023-12-03 22:32:01 24 4
gpt4 key购买 nike

我必须根据与当前位置的距离过滤帖子。附上了下面的代码以供引用,但它不起作用(尽管计算距离工作正常),只有在距离条件似乎不起作用的情况下。此外,当我在条件的顶部尝试距离过滤器时,它会抛出我posts.distance column not found 错误

const posts = await Post.findAll({
where: {
mobile: {
[Op.ne]: mobile,
},
},
attributes:
include: [
[
db.Sequelize.fn(
"ST_Distance",
db.Sequelize.col("location"),
db.Sequelize.fn(
"ST_MakePoint",
parsedLocation.lat,
parsedLocation.lng
)
),
"distance",
],
],
where: {
distance: {
[Op.between]: [
0,100
],
},
},
},
include: {
model: UserProfile,
attributes: ["username"],
},
});

最佳答案

也许如果您将 where 距离条件移出第一个移动设备 where 内的包含条件。
此外,我建议将您的属性移到第一个 where 条件之上。
希望这些更改将修复您的错误或使您走向解决方案。

const posts = await Post.findAll({
attributes:
[[
db.Sequelize.fn(
"ST_Distance",
db.Sequelize.col("location"),
db.Sequelize.fn(
"ST_MakePoint",
parsedLocation.lat,
parsedLocation.lng
)
),
"distance"
]],
where: {
[Op.and]: {
mobile: {
[Op.ne]: mobile,
},
distance: {
[Op.between]: [
0,100
],
}}
},
include: {
model: UserProfile,
attributes: ["username"],
},
});

关于javascript - 使用 sequelize 中的 where 条件根据距离进行过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66125499/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com