Javascript 在 Firebase 中使用 push() 如何获取唯一 ID 并存储在我的数据库中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38768576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:49:39  来源:igfitidea点击:

In Firebase when using push() How do I get the unique ID and store in my database

javascriptreactjsfirebasefirebase-realtime-database

提问by komal deep singh chahal

I am pushing data in firebase, but i want to store unique id in my database also . can somebody tell me,how to push the data with unique id.

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

i am trying like this

我正在尝试这样

  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);
  }

error is "ReferenceError: ref is not defined"

错误是“ReferenceError:ref 未定义”

回答by iOSGeek

You can get the key by using the function key()of any ref object

您可以使用key()任何 ref 对象的函数来获取密钥

There are two ways to invoke pushin Firebase's JavaScript SDK.

  1. using push(newObject). This will generate a new push id and write the data at the location with that id.

  2. using push(). This will generate a new push id and return a reference to the location with that id. This is a pure client-side operation.

Knowing #2, you can easily get a new push id client-side with:

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

You can then use this key in your multi-location update.

push在 Firebase 的 JavaScript SDK 中有两种调用方式。

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

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

知道#2,你可以很容易地获得一个新的推送 id 客户端:

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

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

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

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

If you invoke the Firebase push()method without arguments it is a pure client-side operation.

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

You can then add the key()of the new ref to your item:

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

And write the item to the new location:

newRef.set(newItem);

如果您push()不带参数调用 Firebase方法,则它是纯客户端操作。

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

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

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

并将项目写入新位置:

newRef.set(newItem);

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

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

in your case :

在你的情况下:

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);

}

回答by Ronnie Royston

Firebase v3 Saving Data

Firebase v3 保存数据

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);
}

回答by Azam Alvi

You can get last inserted item id using Promise like this

您可以像这样使用 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));

回答by Anand_5050

Try this . It worked for me

尝试这个 。它对我有用

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

Here res.getKey() will not work but use res.key to get the latest push ID

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

回答by Roger Almeida Leite

It looks like now .push() always returns an Observable. When applied the solutions above I had the following error: "Type 'String' has no compatible call signatures".

看起来现在 .push() 总是返回一个 Observable。应用上述解决方案时,出现以下错误:“类型‘字符串’没有兼容的调用签名”。

That being said, I refer what did work in my case for this new version:

话虽如此,我指的是在我的情况下对这个新版本起作用的内容:

Type 'String' has no compatible call signatures

“字符串”类型没有兼容的调用签名

回答by Manohar Khadka

This worked for me:

这对我有用:

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

See Here: Saving data with firebase.

请参见此处:使用 Firebase 保存数据。