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

标签 javascript angularjs firebase

我确定这是一个微不足道的问题,但似乎无法弄清楚如何访问此 firebase 数组中的玩家 ID。我需要能够访问所有玩家 ID,如果登录时建立的当前 users.id 与玩家 ID 的 firebase 数组之一匹配,那么这些游戏将通过 ng-repeat 循环。我知道如何完成后者,我只是不知道如何访问唯一 ID 中的玩家 ID;希望这是有道理的。非常感谢任何帮助,谢谢。

Firebase

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

关于javascript - 在 firebase 中访问嵌套的唯一键值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31868286/

相关文章:

javascript - Node.js 和 Express : Sequential execution flow one mongodb query request after another

javascript - ngIf 不评估异步代码 Angular 和 Firebase

javascript - 如何在更改时更改克隆元素的值

javascript - 如何配置Angular4路由器的路由

javascript - 使用 ng-repeat 时如何计算行总数?

angularjs - 使用 karma 测试 Angular,文件排序

javascript - 仅当编辑器聚焦于具有 1 个工具栏的多个编辑器时才显示 TextAngular 工具栏

javascript - Firebase 弹出窗口登录弹出窗口被用户关闭错误

android - 不使用默认 FirebaseApp 的 FirebaseInstanceId.getInstance().getToken()

javascript - #each 上的 Handlebars 解析错误 - 每个不匹配 if - 31 :7