【问题标题】:Get the reference path of unique Item Id in Firebase using Ionic使用 Ionic 获取 Firebase 中唯一项目 ID 的参考路径
【发布时间】:2020-08-01 07:17:56
【问题描述】:

我创建了一个使用 UID 将数据发送到 firebase 的函数。数据显示在带有 UID 和项目 ID 的 firebase 中。如果用户登录,我可以检索数据,但我对如何获取具有唯一自动生成 ID 的项目的特定节点子节点的路径感到困惑。

  // Data stored in firebase

  "profiles" : {
  // UID
    "SUFbOOK4XvPNIaQqIjJ2ey6ziDF3"  : {
    //Item ID
      "-M54AgiUdxYEmu20Yau0" : {

      //childs
        "eau" : "111111",
        "edan" : "22222",
        "image" : "0625594",
        "nom" : "hello",
        "prenom" : "world",
        "profession" : "Dev",
        "solde" : "100000",
        "telephone" : "06255444794"
      }
    }

这是该数据库在 userID 之前的路径引用。

this.reference = firebase.database().ref('profiles/'+this.afAuth.auth.currentUser.uid);

这是向 Firebase 发送数据的函数

create(nom,telephone,edan,prenom,profession,eau,solde){

    this.imageName = telephone;
    let loading = this.load.create({
      content: 'Veuillez patienter ...'
    });
    loading.present();
    setTimeout(() => {
      loading.dismiss();
      this.afAuth.authState.take(1).subscribe(auth => {
      this.af.list(`profiles/${auth.uid}`).push({
        nom: nom,
        edan: edan,
        eau: eau,
        prenom: prenom,
        profession: profession,
        telephone: telephone,
        solde: solde,
        image:this.imageName,

      }).then (() => 
      this.navCtrl.push('LoginPage')
      )
      })
    }, 2500);
    this.upload()
  }

如何使用 UID 获取 Firebase 中列表的唯一项 ID 的参考路径?

【问题讨论】:

    标签: javascript firebase firebase-realtime-database angularfire2


    【解决方案1】:

    我想你正在寻找这个:

    reference.once("value", function(snapshot) {
      console.log(snapshot.key); // "SUFbOOK4XvPNIaQqIjJ2ey6ziDF3"
      snapshot.forEach(function(child) {
        console.log(child.key); // "-M54AgiUdxYEmu20Yau0"
        console.log(child.val().nom); // "hello"
        child.child("nom").ref.update("world");
      })
    });
    

    【讨论】:

    • 谢谢!我如何在引用路径中使用 child.key,而不是仅使用 console.log 查看这些值?这实际上是我坚持的步骤。
    猜你喜欢
    • 2016-07-09
    • 2015-05-21
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 2016-07-31
    相关资源
    最近更新 更多