2013-04-01 67 views
0

此刻我有一個我已經存儲在陣列teamIds中的團隊ID列表。我在我的Team貓鼬模式中也有一個函數findByKey,該模式根據密鑰找到一個團隊。正如你可以從代碼中看到的,我必須將每個新的Team.findByKey放在前一箇中,如果我將它放在之前的findByKey之外,它將無法正常工作。JavaScript/mongoose - 循環訪問數組以創建對象陣列

我有問題把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); 

回答

1

可以使用$in運營商來查找您的四支球隊在一個查詢:

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

如果出於某種原因,您需要多次撥打findByKey而不是摺疊把它全部變成一個單一的$in,看看用async庫的方法做得更乾淨。

+0

對不起,我已經添加了「Team' Schema的代碼,你能否看到這是否有幫助?我不太喜歡使用另一個圖書館,因爲我不認爲如果有這樣的方法可以讓我的頭靠近它。任何幫助感謝! – germainelol

+0

@germainelol我更新了我的答案,使用實際的'key'字段名稱。 – JohnnyHK

+0

謝謝,這個工程,我可以輸出一個團隊陣列到頁面,我唯一的問題是這個函數創建的'隊列'數組只能在這個函數中使用。我將如何做到這一點,所以我正在創建一個「隊伍」數組,我可以在整個這個'/ tournament'方法中使用以便稍後使用它? – germainelol