0

目前我有一个存储在数组中的团队 ID 列表teamIds。我的猫鼬模式中还有一个函数findByKeyTeam它可以根据其键找到一个团队。正如您从代码中看到的那样,我必须将每个新的都Team.findByKey放在前一个中,如果我把它放在前一个 findByKey 之外,它就不能正常工作。

我在将Team.findByKey方法放入某种 for 循环时遇到问题,该循环可以遍历数组teamIds和数组中的每个循环Team。我正在开发创建新锦标赛的功能,其想法是您将一组 teamIds 传递到此处,并循环遍历每个 ID,搜索该团队并将其添加到teamsList数组中以创建锦标赛。

我需要将创建Team对象数组的最后一点代码放入我的问题的最后一个Team.findByKey方法中。我将如何解决这个问题,以便我可以传递n多个团队并找到每个团队并将其存储为数组Teams

这是我的代码:

  app.get('/tournament', function(req, res){
    function genMatches(nTeams) {
      var matchArray = [];
      while (nTeams > 1) {
          nTeams = nTeams >> 1;
          var matches = [];
          for (var i = 0; i < nTeams; ++i) {
              matches.push([]);
          }
          matchArray.push(matches);
      }
      return matchArray;
    }

    var teamIds = [
      1364472344972,
      1363173222886,
      1363007586845,
      1363007557484
    ]

    var tournamentName = 'My Tournament';

    Team.findByKey(teamIds[0], function(err, team1) {
        if(err) {
          util.log("Error occured");
        }
        if(!team1) { 
          util.log("The team does not exist");
        }
        Team.findByKey(teamIds[1], function(err, team2) {
          if(err) {
            return util.log("Error occured");
          }
          if(!team2) { 
            return util.log("The team does not exist");
          }
          Team.findByKey(teamIds[2], function(err, team3) {
            if(err) {
              return util.log("Error occured");
            }
            if(!team3) { 
              return util.log("The team does not exist");
            }
            Team.findByKey(teamIds[3], function(err, team4) {
              if(err) {
                return util.log("Error occured");
              }
              if(!team4) { 
                return util.log("The team does not exist");
              }
              var teamList = [
                team1._id,
                team2._id,
                team3._id,
                team4._id
                ];

              var numRounds = Math.log(teamList.length)/Math.log(2);

              var matches = genMatches(teamList.length);
              for(var i = 0; i < matches[0].length; i++){
                matches[0][i] = [ teamList[i+i], teamList[i+(i+1)] ]
              }

              var tournament = new Tournament({
                name: tournamentName,
                teams: teamList,
                rounds: numRounds,
                matches: matches
              });

              res.send(tournament);

            });
          });
        });
      });
  });

团队架构:

'use strict';

var util = require('util');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var validatePresenceOf = function(value){
  return value && value.length; 
};

var toLower = function(string){
  return string.toLowerCase();
};

var getId = function(){
  return new Date().getTime();
};

/**
  * The Team schema. we will use timestamp as the unique key for each team
  */
var Team = new Schema({
  'key' : {
    unique : true,
    type : Number,
    default: getId
  },
  'name' : { type : String,
              validate : [validatePresenceOf, 'Team name is required'],
              index : { unique : true }
            }
});

/**
  * Get complete team details for all the teams
  */
Team.statics.getAll = function(cb){
  var query = this.find({});
  query.sort({key : -1});
  return query.exec(cb);
};

Team.statics.findByKey = function(key, cb){
  return this.find({'key' : key}, cb);
};

Team.statics.findByName = function(name, cb) {
  return this.find({'name' : name}, cb);
};

module.exports = mongoose.model('Team', Team);
4

1 回答 1

1

您可以使用$in运算符在一个查询中查找您的四个团队:

Team.find({key: {$in: teamIds}}, function (err, teams) {
  ...
});

如果由于某种原因您需要findByKey多次调用而不是将其全部折叠成一个$in,请查看使用async库的each方法来更干净地执行此操作。

于 2013-04-01T12:40:09.840 回答