Reputation: 123
I'm sure this is a trivial question but can't seem to figure out how to access the players id in this firebase array. I need to be able to access all the players id's, and if the current users.id established at login matches one of the players id's firebase array then those games will be looped over with ng-repeat. I know how to accomplish the latter, I just can't figure out to access the players id's inside the unique id's; Hopefully that makes sense. Any help is greatly appreciated, thanks.
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;
}
}]);
Upvotes: 3
Views: 2474
Reputation: 598901
You're violating two common Firebase rules here:
I'm guessing #1 happened because you are storing the players using $firebaseArray.$add()
. Instead of repeating myself, I'll list a few questions that deal with the same problem:
The nesting of lists is a common mistake. By having the players stored under each game, you can never load the data for a list of games, without also loading all players for all games. For this reason (and others) it is often better to store the nested list under its own top-level:
games
-Juasdui9
date:
host:
-Jviuo732
date:
host:
games_players
-Juasdui9
-J43as437y239
id: "Simplelogin:4"
name: "Anthony"
-Jviuo732
....
users
Upvotes: 5