从每个房间获取所有成员的详细信息
Get all the members details from each room
With this sort of data:
{
"rooms": {
"one": {
"name": "room alpha",
"type": "private"
},
"two": { ... },
"three": { ... }
},
"members": {
"one": {
"mchen": true,
"hmadi": true
},
"two": { ... },
"three": { ... }
},
...
How can I query all rooms with (by example) the username of each
member in the room?
编辑 2:
我试过让玩家进入游戏并且玩家加载了游戏,这是为游戏加载相关玩家的解决方案。但是我只得到玩家的ID,我需要用户名或个人资料图片url 例如
你看到 players 只显示了它的 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。如何获取游戏玩家的详细信息。
您希望在加载所有游戏时加载玩家。类似于:
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'
With this sort of data:
{ "rooms": { "one": { "name": "room alpha", "type": "private" }, "two": { ... }, "three": { ... } }, "members": { "one": { "mchen": true, "hmadi": true }, "two": { ... }, "three": { ... } }, ...
How can I query all rooms with (by example) the username of each member in the room?
编辑 2:
我试过让玩家进入游戏并且玩家加载了游戏,这是为游戏加载相关玩家的解决方案。但是我只得到玩家的ID,我需要用户名或个人资料图片url 例如
你看到 players 只显示了它的 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。如何获取游戏玩家的详细信息。
您希望在加载所有游戏时加载玩家。类似于:
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'