【问题标题】:How to query Firebase denormalized data with Angularfire?如何使用 Angularfire 查询 Firebase 非规范化数据?
【发布时间】:2017-03-14 02:32:04
【问题描述】:

按照 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 不是函数

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

【问题讨论】:

    标签: angularjs firebase ionic-framework angularfire


    【解决方案1】:

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

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

    【讨论】:

    • 谢谢!我现在在下一行 (snapshot.val().user;) 上未定义。老实说,我不完全理解这段代码的作用......你有没有机会获得 ionic1-firebase 应用程序的完整示例?
    • 我在 Google 上搜索了“firebase 快照 val 用户”并得到了这个:firebase.google.com/docs/reference/js/… 尝试 console.log('snapshot val', snapshot.val()); 在 val() 返回的对象中找到您要查找的内容。
    猜你喜欢
    • 2014-04-19
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-29
    • 2015-08-22
    相关资源
    最近更新 更多