SiD*_*OBS 7 mongoose mongodb node.js
这是我的控制器文件locations.js
var mongoose = require('mongoose');
var Loc = mongoose.model('location');
module.exports.locationsListByDistance = function(req, res) {
var lng = parseFloat(req.query.lng);
var lat = parseFloat(req.query.lat);
var point = {
type: "Point",
coordinates: [lng, lat]
};
var geoOptions = {
spherical: true,
maxDistance: 1000
};
Loc.geoNear(point, geoOptions, function (err, results, stats) {
console.log(results);
});
};
Run Code Online (Sandbox Code Playgroud)
我的模型文件locations.js
var mongoose = require('mongoose');
var reviewSchema = new mongoose.Schema({
author: String,
rating: {
type: Number,
required: true,
min: 0,
max: 5
},
reviewText: String,
createdOn: {
type: Date,
"default": Date.now
}
});
var openingTimeSchema = new mongoose.Schema({
days: {
type: String,
required: true
},
opening: String,
closing: String,
closed: {
type: Boolean,
required: true
}
});
var locationSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
address: String,
rating: {
type: Number,
"default": 0,
min: 0,
max: 5
},
facilities: [String],
// Always store coordinates longitude, latitude order.
coords: {
type: [Number],
index: '2dsphere'
},
openingTimes: [openingTimeSchema],
reviews: [reviewSchema]
});
mongoose.model('location', locationSchema, 'locations');
Run Code Online (Sandbox Code Playgroud)
每当我运行http:// localhost:3000/api/locations?lng = -0.9690884&lat = 51.455041我得到错误geoNear不是函数
TypeError:Loc.geoNear不是Layer.handle上的module.exports.locationsListByDistance(/home/shackers/Projects/mean/loc8r/app_api/controllers/locations.js:51:7)的函数[as handle_request](/ home /shackers/Projects/mean/loc8r/node_modules/express/lib/router/layer.js:95:5)下一步(/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/route.js :137:13)在Route.dispatch(/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/route.js:112:3)在Layer.handle [as handle_request](/ home/shackers) /Projects/mean/loc8r/node_modules/express/lib/router/layer.js:95:5)/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:281:22在Function.process_params(/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:335:12)下一步(/ home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:275:10)在路由器(/ hom)的Function.handle(/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:174:3)e/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:47:12)在Layer.handle [as handle_request](/ home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/layer.js:95:5)在/ home/shackers/Projects /的trim_prefix(/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:317:13)函数.process_params中的/ mean/loc8r/node_modules/express/lib/router/index.js:284:7(/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:335:12 )at/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router下一个(/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:275:10) /index.js:635:15
这是我正在使用的依赖项的版本:
router.get('/', () => {
Loc.aggregate([
{
$geoNear: {
near: 'Point',
distanceField: "dist.calculated",
maxDistance: 100000,
spherical: true
}
}
]).then(function(err, results, next){
res.send();
}).catch(next);
});?
Run Code Online (Sandbox Code Playgroud)
参考: - https://docs.mongodb.com/manual/reference/command/geoNear/
发生此错误是因为.geoNear曾经受支持,但是从Mongoose 5开始不再受支持,后者使用Node MongoDB v3驱动程序。
该问题记录在“ 迁移到Mongoose 5”文档中,该文档又链接到MongoDB 3驱动器发行说明,该发行说明提供了有关建议的替代产品的声明:
geoNear命令的功能在该语言的其他地方,在未分片集合上的$ near / $ nearSphere查询运算符中以及在所有集合上的$ geoNear聚合阶段中都是重复的。
有效地,官方文档支持$geoNear其他答案中所记载的使用方式。
我遇到了完全相同的问题,并且我已经放弃了之前使用的方法(看起来您也遇到了)。以下是不会引发错误的替代方案,并且应该给出与使用 Loc.geoNear 后相同的结果:
Loc.aggregate(
[
{
'$geoNear': {
'near': point,
'spherical': true,
'distanceField': 'dist',
'maxDistance': 1000
}
}
],
function(err, results) {
// do what you want with the results here
}
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4594 次 |
| 最近记录: |