我确定这是一个微不足道的问题,但似乎无法弄清楚如何访问此 firebase 数组中的玩家 ID。我需要能够访问所有玩家 ID,如果登录时建立的当前 users.id 与玩家 ID 的 firebase 数组之一匹配,那么这些游戏将通过 ng-repeat 循环。我知道如何完成后者,我只是不知道如何访问唯一 ID 中的玩家 ID;希望这是有道理的。非常感谢任何帮助,谢谢。
JS
(这是与我的问题相关的一些代码)
game.controller('games.controller', ['$scope', '$state', '$stateParams', 'Auth', '$firebaseArray','Fire', function ($scope, $state, $stateParams, auth, $firebaseArray, fire) {
$scope.games = $firebaseArray(fire.child('games'));
$scope.view = 'listView';
$scope.setCurrentGame = function(game) {
$scope.currentGame = game;
};
$scope.createGame = function() {
if ($scope.format == 'Match Play') {
$scope.skinAmount = 'DOES NOT APPLY';
$scope.birdieAmount = 'DOES NOT APPLY';
}
$scope.games.$add({
name: $scope.gameName,
host: $scope.user.name,
date: $scope.gameDate,
location: {
course: $scope.courseName,
address: $scope.courseAddress
},
rules: {
amount: $scope.gameAmount,
perSkin: $scope.skinAmount,
perBirdie: $scope.birdieAmount,
format: $scope.format,
holes : $scope.holes,
time: $scope.time
}
})
$state.go('games');
};
$scope.addPlayer = function(game) {
$firebaseArray(fire.child('games').child(game.$id).child('players')).$add({
id : $scope.user.id,
name : $scope.user.name,
email : $scope.user.email
});
}
// swap DOM structure in games state
$scope.changeView = function(view){
$scope.view = view;
}
}]);
最佳答案
您在这里违反了两个常见的 Firebase 规则:
- 如果一个实体有一个自然的、唯一的 id,用那个 id 作为它的键来存储它
- 不要嵌套列表
我猜 #1 发生是因为您正在使用 $firebaseArray.$add()
存储玩家。我将列出几个处理相同问题的问题,而不是重复自己:
- Firebase make user object from auth data
- Adding users to firebase
- How to bypass unique ID and reference child nodes
列表的嵌套是一个常见的错误。通过将玩家存储在每个游戏下,您永远无法加载游戏列表的数据,而无需加载所有游戏的所有玩家。出于这个原因(和其他原因),将嵌套列表存储在其自己的顶级下通常更好:
games
-Juasdui9
date:
host:
-Jviuo732
date:
host:
games_players
-Juasdui9
-J43as437y239
id: "Simplelogin:4"
name: "Anthony"
-Jviuo732
....
users
关于javascript - 在 firebase 中访问嵌套的唯一键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31868286/