【问题标题】:In Firebase when using push() How do I get the unique ID and store in my database在 Firebase 中使用 push() 如何获取唯一 ID 并将其存储在我的数据库中
【发布时间】:2016-12-10 15:35:45
【问题描述】:

我在 firebase 中推送数据,但我也想在我的数据库中存储唯一 ID。 有人可以告诉我,如何推送具有唯一 ID 的数据。

我正在尝试这样

  writeUserData() {
    var key= ref.push().key();
    var newData={
        id: key,
        websiteName: this.webname.value,
        username: this.username.value,
        password : this.password.value,
        websiteLink : this.weblink.value
    }
    firebase.database().ref().push(newData);
  }

错误是“ReferenceError: ref is not defined”

【问题讨论】:

  • @Frank van Puffelen 你能看看吗
  • 我记得当您调用push 时,它会为您将唯一键放入数据库中,然后您必须创建一个新孩子并将您的数据放入其中。不过我不确定,我是一名 Android 程序员,所以可能会有所不同。而且弗兰克不会收到通知,因为如果他没有在此帖子上做任何事情,他将不会收到通知

标签: javascript reactjs firebase firebase-realtime-database


【解决方案1】:

试试这个。它对我有用

this.addressRef.push(addressObj).then(res => {
    console.log("address key = " + res.key) ;
});

这里 res.getKey() 不起作用,而是使用 res.key 来获取最新的推送 ID

【讨论】:

    【解决方案2】:

    您可以像这样使用 Promise 获取最后插入的项目 id

    let postRef = firebase.database().ref('/post');
    postRef.push({ 'name': 'Test Value' })
        .then(res => {
            console.log(res.getKey()) // this will return you ID
        })
        .catch(error => console.log(error));
    

    【讨论】:

    • 太棒了,它奏效了。谢谢,但没有得到钥匙。 this.addressRef.push(addressObj).then(res => { console.log("address key = " + res.key) ; });
    • 这应该是正确的答案,因为 .push() 与 .update() 不同。通过更新,节点将被删除并再次创建,使用 .push() 您将添加到节点。
    【解决方案3】:

    这对我有用:

     var insertData = firebase.database().ref().push(newData);
     var insertedKey = insertData.getKey(); // last inserted key
    

    请看这里:Saving data with firebase.

    【讨论】:

      【解决方案4】:

      现在看起来 .push() 总是返回一个 Observable。应用上述解决方案时,出现以下错误:“Type 'String' has no compatible call signatures”。

      话虽如此,我指的是这个新版本在我的案例中的工作原理:

      Type 'String' has no compatible call signatures

      【讨论】:

        【解决方案5】:

        Firebase v3 Saving Data

        function writeNewPost(uid, username, picture, title, body) {
          // A post entry.
          var postData = {
            author: username,
            uid: uid,
            body: body,
            title: title,
            starCount: 0,
            authorPic: picture
          };
        
          // Get a key for a new Post.
          var newPostKey = firebase.database().ref().child('posts').push().key;
        
          // Write the new post's data simultaneously in the posts list and the user's post list.
          var updates = {};
          updates['/posts/' + newPostKey] = postData;
          updates['/user-posts/' + uid + '/' + newPostKey] = postData;
        
          return firebase.database().ref().update(updates);
        }
        

        【讨论】:

        • 我花了一段时间才意识到为什么我的代码不起作用,然后我注意到您的精彩回答中出现了 key; - 我使用的是 key();,就像在 v2 中一样。
        • 我们如何取回这些密钥。这些只是字母数字 ID。我们如何引用它们来读回数据。
        • 这条评论怎么没有获得 100 更多的声誉?我花了 1 个小时才找到这个解决方案!
        【解决方案6】:

        你可以通过任意ref对象的key()函数来获取key

        在 Firebase 的 JavaScript SDK 中有两种方法可以调用 push

        1. 使用push(newObject)。这将生成一个新的推送 id 并将数据写入具有该 id 的位置。

        2. 使用push()。这将生成一个新的推送 id 并返回对具有该 id 的位置的引用。这是一个纯客户端 操作.

        了解 #2,您可以通过以下方式轻松获得新的推送 id 客户端:

        var newKey = ref.push().key();
        

        然后您可以在多位置更新中使用此密钥。

        https://stackoverflow.com/a/36774761/2305342

        如果您在没有参数的情况下调用 Firebase push() 方法,则它是 纯客户端操作。

        var newRef = ref.push(); // this does *not* call the server
        

        然后您可以将新参考的key() 添加到您的项目中:

        var newItem = {
            name: 'anauleau'
            id: newRef.key()
        };
        

        并将项目写入新位置:

        newRef.set(newItem);
        

        https://stackoverflow.com/a/34437786/2305342

        在你的情况下:

        writeUserData() {
          var myRef = firebase.database().ref().push();
          var key = myRef.key();
        
          var newData={
              id: key,
              Website_Name: this.web_name.value,
              Username: this.username.value,
              Password : this.password.value,
              website_link : this.web_link.value
           }
        
           myRef.push(newData);
        
        }
        

        【讨论】:

        • 能否请您通过更改我的代码告诉我。
        • @komaldeepsinghchahal 你需要先声明 ref
        • 你能告诉我......怎么......我没有得到
        • key 作为id 存储在数据对象中是否常见?这就是我的想法,每个模型本身都有一个标识符。但是我遇到的每个 Firebase 示例都没有这样做。这是一种不好的做法吗?
        • 我不得不替换 ref.push().key();使用 ref.push().key;看起来您需要获取属性而不是调用函数。
        猜你喜欢
        • 2013-05-14
        • 2016-05-17
        • 1970-01-01
        • 1970-01-01
        • 2019-03-16
        • 1970-01-01
        • 2020-11-18
        • 1970-01-01
        • 2020-03-06
        相关资源
        最近更新 更多