gpt4 book ai didi

node.js - Mongoose/MongoDb 收到错误 geoNear 不是函数

转载 作者:IT老高 更新时间:2023-10-28 13:18:45 26 4
gpt4 key购买 nike

这是我的 Controller 文件 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);
});
};

我的模型文件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');

每当我运行 http://localhost:3000/api/locations?lng=-0.9690884&lat=51.455041我收到错误 geoNear 不是函数

TypeError: Loc.geoNear is not a function at module.exports.locationsListByDistance (/home/shackers/Projects/mean/loc8r/app_api/controllers/locations.js:51:7) at Layer.handle [as handle_request] (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/layer.js:95:5) at next (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/layer.js:95:5) at /home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:281:22 at Function.process_params (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:335:12) at next (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:275:10) at Function.handle (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:174:3) at router (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:47:12) at Layer.handle [as handle_request] (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:317:13) at /home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:284:7 at Function.process_params (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:335:12) at next (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:275:10) at /home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:635:15

这是我正在使用的依赖版本:

  • Node :8.9.3 npm:5.5.1 express:4.15.5 mongoose:5.0.0mongoDb:3.6.1

最佳答案

router.get('/', () => {
Loc.aggregate([
{
$geoNear: {
near: 'Point',
distanceField: "dist.calculated",
maxDistance: 100000,
spherical: true
}
}
]).then(function(err, results, next){
res.send();
}).catch(next);
});

引用:- https://docs.mongodb.com/manual/reference/command/geoNear/

关于node.js - Mongoose/MongoDb 收到错误 geoNear 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48098106/

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