简体   繁体   English

异步并行最终回调不会触发

[英]Async parallel final callback doesn't fire

I do have a async parallel block to execute two queries into MongoDB. 我确实有一个异步并行块来对MongoDB执行两个查询。 on each step of function(callback) I have a valid return result, no any errors are firing. 在函数的每个步骤(回调)上,我都有一个有效的返回结果,没有引发任何错误。 I did debug and all steps are working except that final callback not firing and I can't event reach with breakpoint to its condition. 我做了调试,所有步骤都在工作,除了最后的回调不触发,而且无法通过断点达到其条件。 Does anybody have idea what that may happen? 有人知道会发生什么吗?

async.parallel([

// first Query
function(callback){
  this._game.findOne({'_id': gameId}, function(err, result){
    if(err){return callback(err);}
    else if(result) {
      callback(null, result);
    }
  });
},

// second Query
function(callback){
  this._player.findOne({'_id': playerId}, function(err, result){
    if(err){return callback(err);}
    else if(result) {
      callback(null, result);
    }
  });
}
],

  // Final callback to send all 2 Results -> which is not working...
  function(error, results){

    if(results){

      res.status(200);
      res.send(results);
    }
  }
);`

You're not dealing with the possibility that Mongo doesn't find any results. 您不应对Mongo找不到任何结果的可能性。 In this case it will call the callback with err and result as null. 在这种情况下,它将使用err调用回调,并且result为null。 When this happens the functions that you're running in parallel don't call their callbacks, so the final function never runs. 发生这种情况时,并行运行的函数不会调用其回调,因此最终函数永远不会运行。 What you do in this case is up to you but you need to call the callback function with something. 在这种情况下,您可以执行什么操作,但是您需要使用一些方法来调用callback函数。

The reason that you're not getting any results is most likely that playerId and gameId are strings and _id is by default an ObjectId. 无法获得任何结果的原因很可能是playerId和gameId是字符串,而_id默认是ObjectId。 Add a var ObjectId = require('mongodb').ObjectId; 添加一个var ObjectId = require('mongodb').ObjectId; , then replace playerId with ObjectId(playerId) and you'll likely see results. ,然后将playerId替换为ObjectId(playerId) ,您可能会看到结果。

This is how i would debug it : 这就是我调试它的方式:

async.parallel([

// first Query
function(fq_callback){
  this._game.findOne({'_id': gameId}, function(fq_err, fq_result){
    if(fq_err){
      console.log('err from _game.findOne');
      return fq_callback(fq_err);
    }
    else if(fq_result) {
      console.log('result from _game.findOne');
      fq_callback(null, fq_result);
    }
  });
},

// second Query
function(sq_callback){
  this._player.findOne({'_id': playerId}, function(sq_err, sq_result){
    if(sq_err){
      console.log('err from _player.findOne');
      return sq_callback(sq_err);
    }
    else if(sq_result) {
      console.log('result from _player.findOne');
      sq_callback(null, sq_result);
    }
  });
}
],

  // Final callback to send all 2 Results -> which is not working...
  function(fn_error, fn_results){
    console.log('in final callback');
    if(fn_error) console.log('we have error');
    if(fn_results){
      console.log('we have results');
      res.status(200);
      res.send(results);
    }else{
      console.log('we have NO results');
    }

  }
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM