【发布时间】:2020-01-09 01:01:14
【问题描述】:
我有一个存储表,其中包含一个 lat_long 列,它将每个存储纬度/经度存储为 sequelize 中的几何点。我想根据来自前端的请求正文的纬度/经度的接近度来搜索商店。已尝试在 Stackoverflow 中利用与此相关的许多相关主题(答案),但了解到查询可以这样编写。
const location = sequelize.literal(`ST_GeomFromText('POINT(${lng} ${lat})', 4326)`);
User.findAll({
attributes: [[sequelize.fn('ST_Distance_Sphere', sequelize.literal('geolocation'), location),'distance']],
order: 'distance',
limit: 10,
logging: console.log
})
.then(function(instance){
console.log(instance);
})
试过了,效果很好,但面临的挑战是;在这个例子和我目前看到的几乎所有样本中,他们使用了只返回距离但我想返回表中所有字段的属性。然后我尝试删除该属性,然后我得到的响应没有根据几何(距离)搜索,我也尝试使用 where 但出现错误。
下面显示的是我最新的查询,它返回错误,请问我该怎么做?谢谢。
export const getNearestStoreWithCategory = async (lat, long) => {
try {
const location = sequelize.literal(`ST_GeomFromText('POINT(${lat} ${long})')`);
const distance = sequelize.fn('ST_Distance_Sphere', sequelize.literal('lat_long'), location)
const storeArr = await Store.findAll({
order: distance,
where: sequelize.where(distance),
limit: 20
})
return storeArr
} catch (error) {
return error
}
}
【问题讨论】:
-
请我仍然需要对此的答案或其他方法来解决它
标签: node.js geolocation sequelize.js