这是模型:
'use strict'; module.exports = () => {
var restaurant = {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
businessCordinates: {
type: Sequelize.GEOMETRY('POINT'),
allowNull: true
}
};
return sequelize.define('restaurant', restaurant); };
所以我试着找到最近的点,以公里为单位。
下面是我使用sequelize和dialect作为mysql的查询:
exports.search = (req, res) => {
let where = {};
if (req.query.lat && req.query.lng && req.query.range) {
var lat = parseFloat(req.query.lat);
var lng = parseFloat(req.query.lng);
query.where = Sequelize.where(
Sequelize.fn('ST_DWithin',
Sequelize.col('businessCordinates'),
Sequelize.fn('ST_SetSRID',
Sequelize.fn('ST_MakePoint',
lat, lng),
4326),
0.032),
true);
// var attributes = Object.keys(db.restaurant.attributes);
// var location = sequelize.literal(`ST_GeomFromText('POINT(${lat} ${lng})')`);
// var distance = sequelize.fn('ST_Distance_Sphere', sequelize.literal('businessCordinates'), location);
// attributes.push([distance, 'distance']);
// query.attributes = attributes;
// query.order = distance;
// query.where = sequelize.where(distance, {
// $lte: req.query.range
// })
}
db.restaurant.findAll(query).then(restaurants => {
return res.page(restaurants);
}).catch(err => {
res.failure(err);
}); };
所以当我试着去像{host}}/api/restaurants这样的餐馆时,lat=40.741895&lng=-73.989308&range=12
此查询会出现如下错误:sqlmessage=function foodway\u db.st\u dwithin不存在
告诉我怎么做?
暂无答案!
目前还没有任何答案,快来回答吧!