Mongoose/MongoDb出现错误,提示geoNear不是一个函数。

8

这是我的控制器文件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不是一个函数”。
以下是我使用的依赖版本:
- node: 8.9.3 - npm: 5.5.1 - express: 4.15.5 - mongoose: 5.0.0 - mongoDB: 3.6.1
TypeError: Loc.geoNear不是一个函数 在module.exports.locationsListByDistance (/home/shackers/Projects/mean/loc8r/app_api/controllers/locations.js:51:7) 在Layer.handle [as handle_request] (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/layer.js:95:5) 在next (/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) 在next (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:275:10) 在Function.handle (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:174:3) 在router (/home/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) 在trim_prefix (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:317:13) 在/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:284:7 在Function.process_params (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:335:12) 在next (/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:275:10) 在/home/shackers/Projects/mean/loc8r/node_modules/express/lib/router/index.js:635:15

2
我遇到了完全相同的问题 - 你找到解决方案了吗? - ptk
12个回答

-1
 index(req, res, next) 
{
   const { lng, lat } = req.query;
   Driver.aggregate([
   {
     '$geoNear': {
                    "near": { 'type': 'Point', 
                    'coordinates': [parseFloat(lng), parseFloat(lat)] },
                    "spherical": true, 
                    "distanceField": 'dist', 
                    "maxDistance": 200000
                }
            }
        ])
            .then(drivers => res.send(drivers))
            .catch(next);
    }

如果您正在学习Stephen Grider在Udemy上托管的课程“MongoDB完整开发者指南”,那么上述代码将完美地运行。请确保将beforeEach属性更改为“.then(() => drivers.createIndexes(...”以避免警告。 - Energetic Pixels

-1
用户phao5814给出的答案非常正确,我尝试了一下,必须说它对我很有效。

欢迎来到StackOverflow。在这里,“我也是”的回复是通过给出好答案的赞,而不是添加新答案来处理的。 - Mark Stosberg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接