I have been having a lot of trouble getting geoNear to work in SailsJS.
In my UserController.js I am trying to make a geoNear query on the location
field in my user model. It is defined as JSON with a 2d-index on it. Here is an example of the field's data:
"location" : { "type" : "Point" , "coordinates" : [ 42.8003 , -73.8808 ]}
In my UserController my geoSearch method looks like this:
geoSearch: function(req, res) {
//sails.log.verbose(req);
var lat = req.param('lat');
var lng = req.param('long');
var limit = req.param('limit') || 50;
var maxDistance = req.param('radius') || 5000; //meters
sails.log.verbose('lat : ' + lat);
sails.log.verbose('lng : ' + lng);
sails.log.verbose('limit : ' + limit);
sails.log.verbose('maxDistance : ' + maxDistance);
if (!(lat && lng)) {
//bad stuff
res.badRequest('Missing lat or long!');
} else {
User.native(function(err, collection) {
collection.geoNear(lng, lat, {
limit: limit,
maxDistance: maxDistance, // in meters
query: {}, // allows filtering
distanceMultiplier: 3959, // converts radians to miles (use 6371 for km)
spherical : true
}, function(mongoErr, users) {
if (mongoErr) {
console.error(mongoErr);
res.send('_geoSearch failed with error='+mongoErr);
} else {
console.log('users=',users);
res.json(users.results);
}
});
});
}
}
However, when I make a request such as:
http://localhost:1337/api/user/location/?lat=42.8003&long=-73.8808
I get the output:
verbose: lat : 42.8003
verbose: lng : -73.8808
verbose: limit : 50
verbose: maxDistance : 5000
{ [MongoError: exception: 'near' field must be point]
name: 'MongoError',
errmsg: 'exception: \'near\' field must be point',
code: 17304,
ok: 0 }
I have been struggling with this for quite a while, any help or guidance would be appreciated.
Other information:
MongoDB ~3.0 SailsJS ~0.10