Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 具有异步的多mongoose调用_Node.js_Mongodb_Asynchronous_Mongoose - Fatal编程技术网

Node.js 具有异步的多mongoose调用

Node.js 具有异步的多mongoose调用,node.js,mongodb,asynchronous,mongoose,Node.js,Mongodb,Asynchronous,Mongoose,我对node和mongoose是新手。 我执行多个查询以查找数据,因此我使用async填充数组,然后将其发送回客户端 我试着从我发现的比赛中获得所有数据 这是我的模式:[团队]1-->N[游戏]1-->N[状态] var json = []; // The variable I will send back async.waterfall([ function( team, next ) { // Find all games from a team (with its id) d

我对node和mongoose是新手。 我执行多个查询以查找数据,因此我使用async填充数组,然后将其发送回客户端

我试着从我发现的比赛中获得所有数据

这是我的模式:[团队]1-->N[游戏]1-->N[状态]

var json = []; // The variable I will send back

async.waterfall([
  function( team, next ) { // Find all games from a team (with its id)
    dbGame.find({ team_id: req.params._id }).exec( next );
  },
  function( games, next ) {
    for (key in games) {   // For all games, finds their stats 
      dbStat.find({ game_id: games[key]._id }).exec(function(err, stats) {
        json.push({
          _id       : games[key]._id,
          stats     : stats
        }); // The json I need to return
      });
      if ( Number(key) === Number(games.length -1) ) {
        next(json);
      }
    }
  }
], function () {
  res.status(200);
  res.json(json);
});
由于异步,变量json send总是空的,我不知道如何获得完整的变量

更新1

好了,很酷,开始工作了

但麻烦的是,所有的统计数据都在对象中的所有json中:

只是一个问题:所有游戏的统计数据都存储在所有json中

[{
 _id:"57cb15f73945ac9808fe5422",
 stats:Array[13]
}, {
 _id:"57ff76fd78ca91a17b25dd1b",
 stats :Array[13]
}]
但我想我可以分类。下面是我的代码:

async.waterfall([
  function(next) { // Find all games from a team (with its id)
    dbGame.find({
      team_id: req.params._id
    }).sort({
      _id: 1
    }).exec(next);
  },
  function(games, next) {
    var gameIds = games.map(function(game) {
      return game._id;
    });
    dbStat.find({
      game_id: {
        $in: gameIds
      }
    }).sort({game_id: 1}).exec(function(err, stats) {
      json = gameIds.map(function (id) {
        return {
          _id: id,
            stats: stats
          }
        });
        next(err, json);
    });
  }
], function(err, json) {
  jsonResponse(res, 200, json);
});
请尝试以下内容:

var json = []; // The variable I will send back

async.waterfall([
    function(team, next) { // Find all games from a team (with its id)
        dbGame.find({
            team_id: req.params._id
        }).sort({
            _id: 1
        }).exec(next);
    },
    function(games, next) {
        var gameIds = games.map(function(game) {
            return game._id;
        });

        dbStat.find({
            game_id: {
                $in: gameIds
            }
        }).sort({game_id: 1}).exec(function(err, stats) {
          json = gameIds.map(function (id) {
            return {
              _id: id,
              stats: stats
            }
          });
          next(err, json);
        });
    }
], function() {
    res.status(200);
    res.json(json);
});
如果它有任何问题,或者我没有测试它而遗漏了什么,请发表评论。

您没有,当您调用nextjson时;您将json作为错误传递,因为第一个参数是错误的。尝试:

async.waterfall([
    function(team, next) { // Find all games from a team (with its id)
        dbGame.find({
            team_id: req.params._id
        }).sort({
            _id: 1
        }).exec(next(err, games));
    },
    function(games, next) {
        var gameIds = games.map(function(game) {
            return game._id;
        });

        dbStat.find({
            game_id: {
                $in: gameIds
            }
        }).sort({game_id: 1}).exec(function(err, stats) {
          json = gameIds.map(function (id) {
            return {
              _id: id,
              stats: stats
            }
          });
          next(err, json);
        });
    }
], function(err, json) {
    res.status(200);
    res.json(json);
});