【问题标题】:Accessing nested unique key values in firebase访问firebase中嵌套的唯一键值
【发布时间】:2015-08-07 01:23:03
【问题描述】:

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

}]);

【问题讨论】:

    标签: javascript angularjs firebase


    【解决方案1】:

    您在这里违反了两条常见的 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
    

    【讨论】:

    • 谢谢,弗兰克。当我将玩家带到顶级水平时,我将如何将他们与他们进入的游戏联系起来?
    • 如果您仔细查看我的示例数据结构,您会发现gamesgames_players 下都存在相同的id。因此,要查找有关游戏的信息,请在/games/$gameid 下查找。要查找它的玩家,请查找 /games_players/$gameid
    • @FrankvanPuffelen firebase 也建议尽可能多地使用平面数据。你的结构不会增加查找次数?
    猜你喜欢
    • 1970-01-01
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多