在 firebase 中访问嵌套的唯一键值

Accessing nested unique key values in firebase

我确定这是一个微不足道的问题,但似乎无法弄清楚如何访问此 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 规则:

  1. 如果一个实体有一个自然的、唯一的 id,用那个 id 作为它的键存储它
  2. 不要嵌套列表

我猜 #1 发生是因为您正在使用 $firebaseArray.$add() 存储玩家。我不会重复自己,而是列出几个处理相同问题的问题:

列表嵌套是一个常见的错误。通过将玩家存储在每个游戏下,您永远无法加载游戏列表的数据,而无需加载所有游戏的所有玩家。出于这个原因(和其他原因),将嵌套列表存储在其自己的顶级下通常更好:

games
    -Juasdui9
        date:
        host: 
    -Jviuo732
        date:
        host: 
games_players
    -Juasdui9
       -J43as437y239
           id: "Simplelogin:4"
           name: "Anthony"
    -Jviuo732
       ....
users