【问题标题】:firebase parent id returned instead of child id返回firebase父ID而不是子ID
【发布时间】:2017-07-19 23:20:48
【问题描述】:

我是 firebase 新手,我也是第一次使用 ionic 来开发聊天应用程序。我有一个类似结构的数据库,如下所示

tutors:
"Ben Clarke":
    email:"ben@yahoo.com"
    name: ben clarke
"James Gall":
   email:james@yahoo.com
   name: james gal

我正在尝试使用电子邮件获取导师的 ID。我尝试使用我在此处和 firebase 文档上找到的指南来执行此操作:

`firebase.database().ref('tutors').orderByChild('email').equalTo(this.myTutor)
  .once('value')
  .then(snapshot => {
    var records = snapshot.key;
    var chilkkey= snapshot.val();
    this.tutorID=records;
    this. uid = chilkkey;
  })

`我希望结果给我“Ben Clarke”孩子的 ID,但它返回的是“tutors”,它是父 ID。请知道我做错了什么。谢谢

【问题讨论】:

    标签: firebase ionic-framework firebase-realtime-database


    【解决方案1】:

    当您对 Firebase 数据库执行查询时,可能会有多个结果。所以快照包含这些结果的列表。即使只有一个结果,快照也会包含一个结果列表。

    如果您随后在快照上请求key,则它是您最初运行查询的位置的键。要获取查询匹配的特定子项/子项的键,您需要遍历结果:

    firebase.database().ref('tutors').orderByChild('email').equalTo(this.myTutor)
      .once('value')
      .then(snapshot => {
        snapshot.forEach(function(child) {
          console.log(child.key);
        });
      })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-20
      • 1970-01-01
      • 2022-11-24
      • 2011-03-21
      相关资源
      最近更新 更多