在Firebase中访问嵌套的唯一键值 [英] Accessing nested unique key values in firebase

查看:120
本文介绍了在Firebase中访问嵌套的唯一键值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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





JS h1>

(这是与我的问题相关的一些代码)



 游戏.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'){
$ scopeskinAmount ='DOES NOT APPLY';
$ scope.birdieAmount ='DOES NOT APPLY';
}
$ scope.games。$ add({
名称:$ scope。游戏名称,
主机:$ scope.user.name,
日期:$ scope.gameDate,
位置:{
课程:$ scope.courseName,
地址: $ scope.courseAddress

规则:{
amount:$ scope.gameAmount,
perSkin:$ scope.skinAmount,
perBirdie:$ scope.birdieAmount,
格式:$ scope.format,
holes:$ scope.holes,
time:$ scope.time
}
})
$ state。去( '游戏');
};
$ b $ 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
});

$ b $ //交换游戏状态下的DOM结构
$ scope.changeView = function(view){
$ scope.view = view;
}

}]);


解决方案

您违反了两个常见的Firebase规则: / b>


  1. 如果一个实体具有一个自然的唯一id,并以该id为关键字存储
  2. 不要嵌套列表

我猜#1是因为你使用 $ firebaseArray 。$添加()。而不是重复自己,我会列出几个问题来处理同样的问题:



列表的嵌套是一个常见的错误。通过让玩家存储在每个游戏下,您不能加载游戏列表的数据,也不会加载所有游戏的所有玩家。出于这个原因(和其他人),最好将嵌套列表存储在自己的顶层:

  games 
-Juasdui9
日期:
主机:
-Jviuo732
日期:
主机:
games_players
-Juasdui9
- J43as437y239
id:Simplelogin:4
名称:Anthony
-Jviuo732
...
用户


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.

JS

(this is some of the code associated with 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;
  }

}]);

解决方案

You're violating two common Firebase rules here:

  1. if an entity has a natural, unique id, store it with that id as its key
  2. don't nest lists

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

这篇关于在Firebase中访问嵌套的唯一键值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆