我已经检查了关于这个主题的所有帖子,并在迭代器中添加了一个回调。然而,它似乎不起作用。
async.forEachOf(scoreTree.nodes, function (node,key, callback){
if(!node.composition.weights){
mongooseCall.etc.find({}
}
,function (err, data) {
//some synchronous code
//use data to update node...
callback(null);
});
}
},function (err) {
lastCall(err, scoreTree, function () {scoreTree.save();});
});
谢谢你的帮助!Marouane。
开发人员控制台是您的朋友,它会在mongooseCall.etc.find({}
之后向您显示代码中可能出现的错误-看起来您的{
太多了
这样试试:
async.forEachOf(scoreTree.nodes, function (node,key, callback){
if(!node.composition.weights){
mongooseCall.etc.find({}
// } <- This is the one too many
,function (err, data) {
//some synchronous code
//use data to update node...
callback(null);
});
}
},function (err) {
lastCall(err, scoreTree, function () {scoreTree.save();});
});
实际上,我在对mongodb的调用中删除了一些代码,我忘记了一个}。这里的主要问题是没有为所有节点调用"回调"。添加else,修复了问题。
async.forEachOf(scoreTree.nodes, function (node,key, callback){
if(!node.composition.weights){
mongooseCall.etc.find({}
// } <- This is the one too many
,function (err, data) {
//some synchronous code
//use data to update node...
callback(null); //not called in all the cases
});
}else{
//this needs to be added
callback(null);
}
},function (err) {
lastCall(err, scoreTree, function () {scoreTree.save();});
});