将variables传递到node.js mongoose查找查询
我希望你们能帮忙 我对node.js / mongodb相当陌生,而且在将variables传递给mongoose模型查询时遇到了问题。
如果我运行这个,作为string手动传递经度/纬度(-0.18,51.24),那么它完美的工作,并从数据库中返回大量的对象。
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'); }); });
但是,如果我尝试并将这些坐标作为variables传入此函数运行它不会返回任何结果:
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! }); });
}
我敢肯定,我只是在做一些简单的错误,但是我一直在摸索着这一段时间,并且无法摆脱它。 任何帮助将非常感激!
谢谢。
尝试做get.longitude = parseFloat(get.longitude)
和get.latitude = parseFloat(get.latitude)
。
你也可以在每个variables的前面加一个“+”号来强迫它们变成数字,
{ $nearSphere : [+get.longitude, +get.latitude]
即使其中一个variables是负的,也可以工作。