Mongoose/MongoDb getting error geoNear is not a fu

2020-07-08 07:23发布

This is my controller file 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);
  });
};

My model file 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');

Whenever I run http://localhost:3000/api/locations?lng=-0.9690884&lat=51.455041 I get error geoNear is not a function

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

This are versions of dependencies i am using:

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

10条回答
\"骚年 ilove
2楼-- · 2020-07-08 07:47
router.get('/', () => {
    Loc.aggregate([
        {
            $geoNear: {
                near: 'Point',
                distanceField: "dist.calculated",
                maxDistance: 100000,
                spherical: true                
            }
        }
    ]).then(function(err, results, next){
        res.send();
    }).catch(next);
});

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

查看更多
萌系小妹纸
3楼-- · 2020-07-08 07:52
 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);
    }
查看更多
不美不萌又怎样
4楼-- · 2020-07-08 07:55
router.get('/',function(req,res,next){
    Loc.aggregate([
        {
            $geoNear: {
                near: {type:'Point', coordinates:[parseFloat(req.query.lng), parseFloat(req.query.lat)]},
                distanceField: "dist.calculated",
                maxDistance: 1000,
                spherical: true                
            }
        }
    ]).then(function(Locs){
        res.send(Locs)
    })
})
查看更多
▲ chillily
5楼-- · 2020-07-08 08:01

I'm having the exact same problem and I've abandoned the approach I was using before (which looks like you were having too). The following is an alternative that does not throw an error and should give you the same result you were after using Loc.geoNear:

Loc.aggregate(
        [
            {
                '$geoNear': {
                    'near': point,
                    'spherical': true,
                    'distanceField': 'dist',
                    'maxDistance': 1000
                }
            }
        ],
        function(err, results) {
            // do what you want with the results here
        }
    )
查看更多
登录 后发表回答