1

I hope you guys can help. I'm fairly new to node.js / mongodb and i'm having trouble passing in variables to a mongoose model query.

If I run this, passing in the longitude/latitude (-0.18, 51.24) manually as strings then it works perfectly and returns lots of objects from the database.

Venue.find( { location : { $nearSphere : [-0.18, 51.24], $maxDistance : 25/3959 } }, null, {limit: 50}, function(err, results){
    results.forEach(function(result){
       console.log('Found a record');
    });
}); 

However if I try and pass in these coordinates as variables into the function that this runs in it doesn't return any results:

function generateWorld(get) {

    console.log('Generating for x:' + get.longitude + ' y:' + get.latitude); // Console logs correct coords

    Venue.find( { location : { $nearSphere : [get.longitude, get.latitude], $maxDistance : 25/3959 } }, null, {limit: 50}, function(err, results){
        results.forEach(function(result){
           console.log('Found a record'); // <-- This doesn't return any results!
        });
    }); 

}

I'm sure I'm just doing something simple wrong but I've been scratching my head over this for a while and can't get past it. Any help would be much appreciated!

Thanks.

Paul Maddox
  • 15
  • 1
  • 4

2 Answers2

2

You could also just put a '+' sign in front of each variable to coerce them to numbers, i.e.,

{ $nearSphere : [+get.longitude, +get.latitude]

Works even if one of the variables is negative.

danmactough
  • 5,444
  • 2
  • 21
  • 22
1

Try to do get.longitude = parseFloat(get.longitude) and get.latitude = parseFloat(get.latitude).

alessioalex
  • 62,577
  • 16
  • 155
  • 122