async.js : forEachOf 没有调用最后一个回调

async.js : forEachOf is not calling the last callback

我已经检查了关于该主题的所有帖子,并在迭代器中添加了一个回调。 但是,它似乎不起作用。

  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();});
       });

感谢您的帮助! 马鲁安。

开发人员控制台是您的好帮手,它会向您显示代码中可能存在的错误 - 看起来您有 1 个错误 { - 就在您的 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 的调用中删除了一些代码,我忘记了一个 }。 这里的主要问题是 "callback" 没有被所有节点调用。 添加 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();});
       });