【问题标题】:Firebase realtime database: how to get the value of a parent nodeFirebase 实时数据库:如何获取父节点的值
【发布时间】:2020-05-12 13:51:31
【问题描述】:

假设我有如下结构的数据库节点:每个用户设置他/她偏好的用户名,并且由 Firebase auth 分配的他/她的 UID 记录在用户名下。

"uid_match":
 {
   "username_1" : {
      "uid" : "firebase_uid_1"
    },
   "username_2" : {
      "uid" : "firebase_uid_1"
  }

我正在尝试编写代码以通过 firebase uid 提取用户名。它将查找某个 firebase uid 并返回用户名,该用户名具有自己的 URL。

以下是我编写的部分代码,但它返回的是“/uid_match”,而不是“/username_1”或“/username_2”。

 let ref=firebase.database.ref('/uid_match');
 let user=firebase.auth().currentUser;
 ref.orderByChild('uid').equalTo(user.uid).once('value', function(snapshot) {
     window.location.href='/'+snapshot.key;
 })

问题:提取“username_1”或“username_2”而不是“uid_match”的正确查询代码是什么?提前非常感谢。

【问题讨论】:

    标签: javascript firebase firebase-realtime-database


    【解决方案1】:

    您需要在snapshot 内部进行迭代,以检索键username_1_username_2

     let ref=firebase.database.ref('/uid_match');
     let user=firebase.auth().currentUser;
     ref.orderByChild('uid').equalTo(user.uid).once('value', function(snapshot) {
         snapshot.forEach(function(childSnapshot){
         window.location.href='/'+childSnapshot.key;
       });
     });
    

    【讨论】:

      猜你喜欢
      • 2020-04-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-11
      • 2017-03-26
      • 1970-01-01
      • 1970-01-01
      • 2020-10-02
      相关资源
      最近更新 更多