我有一个问题已经花费了我几天的时间,但我完全无法理解。我使用 node.js 并有一个 mongodb,其中的位置索引为它们的坐标。我事先验证了索引工作正常。对于一组坐标,我想找到最近的地方。以下是我尝试的方法:
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
为
... { $near: [ 10, 10 ] } ...
我始终没有错误和正确的地方。如果我避免调用coordFinder
ingetCoords
并更改getCoords
为
function getCoords(userId, callback) {
callback(
null,
[
// some coordinates
]
);
我知道该错误表明堆栈溢出(增加堆栈大小没有帮助),但我不知道我是如何造成的。有谁知道为什么会发生这种情况?提前感谢您的见解!
编辑mongodb-native
:顺便说一句,直接使用时遇到了同样的问题,所以mongoose
似乎不是问题。另一个线索 - 当我不在回调中调用它时,使用相同的查询永远不会给我带来问题。这可能是错误的来源吗?
我用
- 节点.js 0.6.14
- MongoDB 2.2.2
- 节点模块异步 0.1.22
- 节点模块 mongodb 1.2.5
- 节点模块猫鼬2.5.7
干杯,乔治