从每个房间获取所有会员详细信息

时间:2015-03-30 23:46:45

标签: javascript angularjs data-structures firebase

  

有了这种数据:

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

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

    ...
     

如何查询所有房间(例如)每个房间的用户名   会员在房间里?

EDIT2:

我试图让玩家加入游戏并且玩家装满了游戏,这是一个加载游戏相关玩家的解决方案。但我只获得了玩家的ID,我需要用户名或个人资料图片网址。

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。如何通过游戏获取玩家的详细信息。

1 个答案:

答案 0 :(得分: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'