【发布时间】:2017-10-16 05:06:01
【问题描述】:
使用Sequelize和地理空间查询,如果我想找到离某个位置最近的“n”个点,Sequelize 查询应该如何?
假设我有一个看起来像这样的模型:
sequelize.define('Point', {geo: DataTypes.GEOMETRY('POINT')});
现在假设我们通过以下方式在数据库中输入 100 个随机点:
db.Point.create({geo: {type: 'Point', coordinates: [randomLng,randomLat]}});
假设我们有一个lat 和lng 变量来定义一个位置,并且我们想要找到离它最近的 10 个点。当我运行这个查询时,我得到一个错误:
const location = sequelize.literal(`ST_GeomFromText('POINT(${lat} ${lng})', 4326)`);
db.Point.findAll({
attributes: [['distance', sequelize.fn('ST_Distance', sequelize.col('Point'), location)]],
order: 'distance',
limit: 10
});
// -> TypeError: s.replace is not a function
知道是什么问题/如何解决吗?
谢谢!
【问题讨论】:
-
我不确定,但看起来你的属性周围有多余的方括号
-
这个有什么消息吗?
标签: mysql postgresql sequelize.js geospatial postgis