2015-03-30 68 views
0

所有成員細節有了這樣的數據:獲得的每間客房

{ 
    "rooms": { 
     "one": { 
     "name": "room alpha", 
     "type": "private" 
     }, 
     "two": { ... }, 
     "three": { ... } 
    }, 

    "members": { 
     "one": { 
     "mchen": true, 
     "hmadi": true 
     }, 
     "two": { ... }, 
     "three": { ... } 
    }, 

    ... 

我如何可以查詢所有客房都以(舉例)的每名成員 的用戶名?

EDIT2:

我試圖把玩家的遊戲和玩家加載遊戲,這是加載了相關選手進行遊戲的解決方案。但我只能得到玩家的ID,我需要用戶名或者個人資料圖片url。

enter image description here

你看,玩家僅通過其ID顯示,因爲我救了它這樣: this.root.child('games').child(gameID).child('players').child(this.currentUser.id).set(true);

,但是當我得到的遊戲:

this.root.child('games').on('value', function(snapshot) { 
    console.log(snapshot.val()); 
}); 

我只得到了玩家當然是id。我如何獲得遊戲玩家的詳細信息。

+0

如果你想加載所有房間的所有成員,我可能會簡單地加載整個數據樹高一級。但我的感受並不是你想要的。你可以編輯*你的問題(使用正確的編輯鏈接),並澄清你想要完成什麼以及你已經嘗試了什麼? – 2015-03-31 02:35:26

+0

我已經編輯更詳細的答案 – user2579150 2015-03-31 17:19:51

+0

你已經嘗試過什麼嗎?如果是這樣,請發佈您嘗試的內容(請參閱http://stackoverflow.com/help/mcve)。這也會解決許多缺失的信息,例如:您的目標環境是什麼。 – 2015-03-31 17:35:04

回答

0

您正在尋找加載所有遊戲的玩家,當這些遊戲加載。例如:

this.root.child('games').on('value', function(gamesSnapshot) { 
    console.log(gamesSnapshot.val()); 
    var loadedPlayers = {}; 
    gamesSnapshot.forEach(function(gameSnapshot) { 
    var game = gameSnapshot.val(); 
    Object.keys(game.players).forEach(function(playerId) { 
     if (Object.keys(loadedPlayers).indexOf(playerId) == -1) { 
     this.root.child('players').child(playerId).once(function('value', function(playerSnapshot) { 
      loadedPlayers[playerId] = playerSnapshot.val(); 
     }); 
     } 
    }); 
    }); 
}); 

所以這個代碼非常蠻力加載所有遊戲中的所有玩家。

儘管在很多情況下它可能運行良好,但您應該始終考慮是否不如更好地組織您的數據,使其更符合應用程序的需求。例如:不是加載整個玩家,您也可以將每個玩家需要顯示的數據存儲在他們參與的遊戲中。所以如果你需要顯示玩家的名字:

games 
    -J436712379 
    creator: 'ismaelaa' 
    players 
     simplelogin:35: 'Frank van Puffelen'