在我的firebase数据中,我有一个游戏对象和一个玩家对象。嵌套在玩家对象中的是与游戏唯一键匹配的唯一键,这些是包含玩家的游戏。然后嵌套在游戏中的唯一键(在玩家对象内)是进入该特定游戏的实际玩家的键。我想要完成的只是当前用户(player.uid)加入的游戏的重复次数。因此,在下面的代码中,我检查玩家ID(在玩家对象内)是否与他们的火力基础UID匹配,如果是,我将其推入空数组,那么循环应该迭代该数组并且如果数组中的键与游戏对象中的键匹配...则返回true;从“隐藏游戏”中切换班级,其中显示为无,以显示游戏'它有一个块的显示。如果我将键添加到已注释掉的gamesToShow数组中,但这不适用于应该从循环中填充的实际gamesToShow数组中。我在这做错了什么?我试过移动gamesToshow数组,但它仍然记录为空。为了使它在for循环中可用,我需要做什么?在此先感谢,我发布了相关代码,如果有任何其他需要让我知道。谢谢大家。
"games":{
"-JwYx6ckhITt2GWOmzLy":{
"date":"8/27/2015",
"host":"Anthony DeVenuto",
"hostEmail":"anthonydevenuto@gmail.com",
"location":{
"address":"1234 Crooked Rd, Chicago Illl",
"course":"Crooked Stick"
},
"name":"Crooked Stick Run",
"rules":{
"amount":"21",
"format":"Match Play",
"holes":"9",
"perBirdie":"DOES NOT APPLY",
"perSkin":"DOES NOT APPLY",
"time":"12:00pm"
}
},
"-Jwi64w0weox4vxIbz8J":{
"date":"8/23/2015",
"host":"Anthony DeVenuto",
"hostEmail":"anthonydevenuto@gmail.com",
"location":{
"address":"1234 fdsadgad",
"course":"West Hills Gathering"
},
"name":"West Side Shuffle",
"rules":{
"amount":"21",
"format":"Match Play",
"holes":"18",
"perBirdie":"DOES NOT APPLY",
"perSkin":"DOES NOT APPLY",
"time":"1:00pm"
}
},
"-Jwx-f7HnjIKdkMnM16D":{
"date":"8/23/2015",
"host":"Andy",
"hostEmail":"andy@andy.com",
"location":{
"address":"1234 First Ave",
"course":"WestCode Hills"
},
"name":"WestCode Hustle",
"rules":{
"amount":"12",
"format":"Match Play",
"holes":"18",
"perBirdie":"DOES NOT APPLY",
"perSkin":"DOES NOT APPLY",
"time":"1:00pm"
}
}
},
"players":{
"-JwYx6ckhITt2GWOmzLy":{
"-Jx1uw6iY87HoNJfAngF":{
"email":"andy@andy.com",
"id":"simplelogin:19",
"name":"Andy"
}
},
"-Jwi64w0weox4vxIbz8J":{
"-Jx1uxoJ0H8Pycp7V12s":{
"email":"andy@andy.com",
"id":"simplelogin:19",
"name":"Andy"
}
},
"-Jwx-f7HnjIKdkMnM16D":{
"-Jx1nbKxyLcbwFFIGjh4":{
"email":"anthonydevenuto@gmail.com",
"id":"simplelogin:22",
"name":"Anthony DeVenuto"
}
}
},
"users":{ }
}
var player = auth.$getAuth();
$scope.displayGames = function(game){
var gamesToShow = [];
// var gamesToShow = ['-JwYx6ckhITt2GWOmzLy', '-Jwi64w0weox4vxIbz8J'];
var playersRef = fire.child('players');
playersRef.on('value', function(snapshot){
var gamesObjects = snapshot;
gamesObjects.forEach(function(snapshot){
var gameKeys = snapshot.key()
var playerKeys = snapshot;
playerKeys.forEach(function(snapshot){
if (snapshot.val().id == player.uid) {
gamesToShow.push(gameKeys);
console.log(gameKeys)
}
});
});
});
for (var i=0;i<gamesToShow.length;i++) {
var uniqueKeys = gamesToShow[i];
if (game.$id == uniqueKeys) {
return true;
}
};
}
<h1>Dashboard</h1>
<ul>
<li class="hideGames" ng-repeat="game in games" ng-class="{showGames:displayGames(game)}">
<a href="#" ng-click="gameDetails(game)">Course: {{ game.name }} <br> Date:{{ game.date }}<br></a>
<a href="#" ng-click="removeDashboardGame(game)">Remove</a>
</li>
</ul>
答案 0 :(得分:1)
如果我正确解析,您正在尝试获取当前用户正在播放的游戏列表。如果是这样,此代码将起作用:
var uid = 'simplelogin:19';
var gamesToShow = [];
ref.child('players').on('value', function(gamesSnapshot) {
gamesSnapshot.forEach(function(gameSnapshot) {
var playerKeys = Object.keys(gameSnapshot.val());
playerKeys.forEach(function(playerKey) {
var player = gameSnapshot.val()[playerKey];
if (player.id === uid) {
console.log(gameSnapshot.key());
gamesToShow.push(gameSnapshot.val());
}
});
});
console.log(gamesToShow);
});
显示此代码的JSBin:http://jsbin.com/selisa/edit?js,console
但并不是说你的数据结构对于你想要完成的目标来说是非常糟糕的。你正在通过玩家循环以通过他们的键来匹配他们,这使事情变得复杂。由于玩家拥有现有的自然键(他们的uid)并且每个玩家可能只能加入每个游戏一次,所以最好将玩家存放在他们的游戏中的游戏中:
{
"-JwYx6ckhITt2GWOmzLy": {
"simplelogin:19": true
},
"-Jwi64w0weox4vxIbz8J": {
"simplelogin:19": true
},
"-Jwx-f7HnjIKdkMnM16D": {
"simplelogin:22": true
}
}
使用此数据结构,您只需使用Firebase查询获取游戏列表:
ref.child('players')
.orderByChild('simplelogin:19')
.equalTo(true)
.once('value', function(snapshot) {
console.log(snapshot.val());
});
通常情况下,您可以通过针对打算如何使用数据结构优化数据结构来避免许多麻烦。
您可能需要考虑的另一个选择是将每个播放器/用户的游戏存储在其/ users节点下。这样你甚至不需要查询,但你可以直接访问游戏:
ref.child('users').child('simplelogin:19').child('games').once('value', ...
这将是性能最佳的解决方案,因为它甚至不需要查询来访问游戏。