【问题标题】:How get key from query Firebase in Javascript如何从 Javascript 中的查询 Firebase 中获取密钥
【发布时间】:2019-02-10 19:41:28
【问题描述】:

如何获取UID等于firebase.auth().currentUser.uid的key,因为我可以在用户输入新数据时更新这个信息。

如何更新信息?我使用 orderByChild 和 equal 并设置,但不起作用。

我的代码:

updateUserInformation() {
  var ref = firebase.database().ref('/users/');
  ref.on('value', (snapshot) => {
    snapshot.forEach((child) => {
      if (child.val().UID == firebase.auth().currentUser.uid) {
        this.profileData = [{
          name: child.val().name,
          lastname: child.val().lastname,
          phone: child.val().phone,
          direction: child.val().direction,
          followQuantity: child.val().followQuantity,
          points: child.val().points,
          sex: child.val().sex,
        }];
        this.cdRef.detectChanges();
      }
    });
  })
}

【问题讨论】:

    标签: javascript angular firebase firebase-realtime-database


    【解决方案1】:

    假设您的实时数据库中有唯一的 UID 字段,您可以这样做:

    updateUserInformation() {
      const uid = firebase.auth().currentUser.uid;
      var ref = firebase.database().ref('/users/').orderByChild('UID').equalTo(uid);
      ref.once('value', (snapshot) => {
        const updates = {};
        snapshot.forEach((child) => {
          const userKey = child.key;
          const userObject = child.val();
          updates[`${userKey}/fieldWhichYouWantToUpdate`] = `Field Value you want it to set to`;
          firebase.database().ref('/users/').update(updates);
        });
      })
    }
    

    我没有对此进行测试,但我认为这应该可行。

    【讨论】:

    • 对不起,我要更新firebase数据库中的信息,this.profileData是错误的,不知道你是否理解我。
    • @JuanmaPerez,请尝试更新的答案。我想这就是你要找的。​​span>
    • 如果你使用 @angular/fire BTW 会更容易。
    【解决方案2】:

    这里是 Firestore 和 ES6 Promises 的示例。如果您考虑切换到 Firestore,查询将如下所示:

        updateUserData = (user) => {
    
        return new Promise((resolve, reject) => {
            firebaseService.firestore()
                .collection('users')
                .doc(user.id)
                .update({
                    name: user.name,
                    lastname: user.lastname,
                    ...
                    })
                .then(() => {
                    resolve();
                })
                .catch(function (error) {
                    reject(error);
                });
        });
    }
    

    【讨论】:

      【解决方案3】:

      @SiddAjmera 的回答是正确的。它还显示了您当前数据模型的一个缺点:可能有多个节点具有正确的 UID。虽然您的应用程序可能不允许这样做,但您的数据模型允许这样做,并且您无法通过安全规则强制 UID 的唯一值。

      当您存储具有自然标识符的项目时,请考虑使用该自然 ID 作为其键来存储这些项目。对于用户,这意味着您应该强烈考虑将用户存储在他们的 UID 中。

      users: {
        "BD21...Wv33": {
          "email": "test2@test.com",
          ...
        }
      }
      

      这个结构:

      • 您可以保证每个用户的 UID 只能出现一次,因为根据定义,键在父节点中是唯一的。
      • 您可以通过用户的 UID 简单地查找用户:

        var ref = firebase.database().ref('/users/').child(uid);
        ref.once('value', (snapshot) => {
          console.log(snapshot.key);
          snapshot.ref.child("fieldWhichYouWantToUpdate"].set("Field Value you want it to set to");
        })
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-08
        • 1970-01-01
        • 2017-12-05
        • 2022-11-06
        • 2017-03-25
        • 2017-09-22
        • 1970-01-01
        • 2023-03-18
        相关资源
        最近更新 更多