尝试使用mongoose / mongodb-native执行mongodb查询时出现RangeError错误

我有一个问题已经花了我几天,我完全不明白这一点。 我使用node.js,并有一个mongodb与他们的坐标索引的地方。 我证实,索引工作正常。 对于一组坐标,我想find最近的地方。 以下是我试图去做的事情:

var Places = require(./models/places.js), coordFinder = require('./coordFinder.js'); ... function getCoords(userId, callback) { coordFinder.getCoords(userId, function(err, coords) { if (err) { callback(err); } else { callback(null, coords); } } } function getNearestPlaces(coords, callback) { async.forEachSeries( coords, function(coord, done) { Places.findOne( { coordinates: { $near: [ coord.lat, coord.lon ] } }, function(err, place) { // do something with place done(); } ) }, function(err) { callback(err ? err : null); } ); } ... async.waterfall( [ ... getCoords, getNearestPlaces, ... ], function(err) { ... } } 

对于第一个查询,mongodb查询始终失败,并显示以下错误消息:

 RangeError: Maximum call stack size exceeded 

当我将getNearestPlaces的查询getNearestPlaces

 ... { $near: [ 10, 10 ] } ... 

我始终没有错误和正确的地方。 如果我避免在coordFinder中调用coordFinder getCoords更改为相同的情况

 function getCoords(userId, callback) { callback( null, [ // some coordinates ] ); 

我知道错误表明一个堆栈溢出(增加堆栈大小没有帮助),但我不知道我怎么能造成一个。 有没有人有一个线索,为什么会发生这种情况? 提前感谢您的见解!

编辑 :当直接使用mongodb-native时,也遇到同样的问题,顺便说一下,所以mongoose似乎不是问题。 还有一个线索 – 使用相同的查询从来没有给我一个问题,当我不从一个callback调用它。 这可能是一个错误的来源?

我用

  • node.js 0.6.14
  • mongodb 2.2.2
  • 节点模块asynchronous0.1.22
  • 节点模块mongodb 1.2.5
  • 节点模块mongoose 2.5.7

干杯,乔治亚

这个问题很可能来自于async.forEachSeries的隐藏recursion。 尝试使用setTimeout包装Places.findOne调用,如下所示:

 function getNearestPlaces(coords, callback) { async.forEachSeries( coords, function(coord, done) { setTimeout(function () { Places.findOne( { coordinates: { $near: [ coord.lat, coord.lon ] } }, function(err, place) { // do something with place done(); } ); }, 1); }, function(err) { callback(err ? err : null); } ); } 

这将把Places.findOne调用放在Places.findOne的调用栈async.forEachSeries

姆姆指着我正确的方向。 显然,传递给mongoose查询的Number实例导致堆栈溢出。 我不知道为什么,但从旧的(如mjhmbuild议)创build一个新的数字对象或将数字转换为带有valueOf()的原语解决了这个问题。

如果有人知道原始数字对象如何导致堆栈溢出,我非常感谢您可能提供的任何见解。