Accessing nested unique key values ​​in firebase

I'm sure this is a trivial question, but can't seem to figure out how to access the id of players in this firebase array. I need to be able to access all player ids, and if the current users.id set on login matches one of the player ids firebase arrays, then these games will loop over the ng-repeat. I know how to accomplish the latter, I just can't figure out how to access the player id inside the unique id; Hope this makes sense. Any help is greatly appreciated, thanks.

Firebase

Js

(this is part of the code related to my problem)

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;
  }

}]);

      

+3


source to share


1 answer


You are breaking two general Firebase rules:

  • if the object has a natural unique id, store it with that id as a key
  • do not make lists

I'm guessing # 1 happened because you store players using $firebaseArray.$add()

. Instead of repeating myself, I have listed several questions that address the same problem:



Nesting lists is a common mistake. With players stored in each game, you can never load data for a playlist without also downloading all players for all games. For this reason (and others) it is often better to keep 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

      

+4


source







All Articles