如何使用Angularfire查询Firebase非规范化数据?

时间:2017-03-08 16:21:28

标签: angularjs firebase ionic-framework angularfire

按照Firebase官方文档,我构建了这样的数据:

 {
  "contests": {
    "1": {
      "start_time": "2016-01-07T05:46:27 -02:00",
      "end_time": "2016-08-15T02:00:57 -03:00",
      "title": "The Best Voice ever!",
      "category": "music",
      "videos": {
        "video_id_44": true
      },
      "users": {
        "lgraves": true
      }
    },
    ....
  },
  "users": {
    "lgraves": {
      "p_name": "Lorie",
      "l_name": "Graves",
      "email": "loriegraves@zaggle.com",
      "location": "Montana",
      "image_url": "https://randomuser.me/api/portraits/men/85.jpg",
      "created_time": "2014-12-22T03:40:38 -02:00",
      "videos": {
        "video_id_44": true
      }
    },
    ....
  },
  "videos": {
    "video_id_44": {
      "video_url": "www.cdn.video.com/9d0c60af-a069-484e-987f-fad9935dc9a7",
      "users": {
        "fhenderson": true
      }
    }, ...
  }
}

我的控制器:

var vm = this;
var contestsRef = firebase.database().ref().child("contests");
vm.contests = $firebaseArray(contestsRef);      // synchronize the object with a three-way data binding

contestsRef.on("value", function(snapshot) {
        var key = snapshot.key();  /// error: snapshot.key is not a function
        var childKey = snapshot.val().user;
        ref.child("users/" + childKey + "/name").once('value', function(snapshot) {
        });
      });

在竞赛中使用ng-repeat进行迭代时,我得到错误: FIREBASE警告:用户回调引发了异常。 TypeError:snapshot.key不是函数

我的问题是:在迭代“竞赛”数组时如何访问“用户”和“视频”,并且仍在同步“竞赛”数组所做的任何更改?

1 个答案:

答案 0 :(得分:1)

Javebratt(Jorge)说尝试从key()中删除括号:

var key = snapshot.key; // not snapshot.key()