问题
I am trying to create a route to retrieve the records with nearby location coordinates. I checked the logs and found the "docs" value as undefined. What am I going wrong with?
router.get('/userlist/:lat/:lng', function (req, res) {
var db = req.db;
var lat = req.params.lat, lng = req.params.lng;
console.log("lat "+lat);
console.log("lng "+lng);
var collection = db.get('userlist');
collection.find({
"location": {
$nearSphere: {
$geometry: {
type: "Point" ,
coordinates: [ lat , lng ]
}
}
}
}, {}, function (e, docs) {
docs = JSON.stringify(docs);
console.log("docs "+docs);
res.send(docs);
});
});
I am storing the JSON data as shown in the screenshot below.
回答1:
The location should be stored in the format:-
"location" : {
"type" : "Point",
"coordinates" : [
-70.845654,
42.146249
]
}
The geojson point should be an object and not an array.
回答2:
As @Webdev said, the coordinates are stored as strings and shall not be. But I was unable to store them in numeric form using mongoose. So I am parsing the latitude and the longitude into float in the find query itself and it works.
来源:https://stackoverflow.com/questions/59840828/find-method-in-mongodb-returns-undefined