2015-12-20 18 views
0

我試圖把幫助返回數組的文件,它看起來像:我如何才能找到文件基於陣列集合中值

documents = { 
    realPlayers: [], 
    subscribers: [ 
       {playerId: }, 
       {playerId: }, 
       {playerId: } 
       ], 
    privatGame:, 
    gameType:, 
    gameStatus: 'active' 
} 

我嘗試這樣做,但它不工作(的forEach太):

Template.myGames.helpers({ 
    'myGames': function() { 
     let gamePul = [], 
      activeGames = Games.find({gameStatus: "active"}); 
      for (var i = 0; i < activeGame.length; i++) { 
       if (activeGame[i].subscribers) { 
       for (var j = 0; j < activeGame[i].subscribers.length; j++) { 
        if (activeGame[i].subscribers[j].playerId = Meteor.userId()) gamePul.push(activeGame[i]); 
       } 
       } 
      }; 
      return gamePul; 
    } 
}); 
+0

你怎麼回來了'activeGames'?應該是一個可以使用'.forEach'的遊標。你有什麼結果嗎? –

回答

0

你只是想找到一組遊戲當前用戶是訂閱者的權利?你只需要$elemMatch在查詢:

Template.myGames.helpers({ 
    myGames: function() { 
    return Games.find({ gameStatus: "active", 
     subscribers: { $elemMatch: { playerId: Meteor.userId() }}}); 
    } 
}); 

docs

+0

是的,你完全正確,它完全正常工作!謝謝!) –