45

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.

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"

komal deep singh chahal
  • 1,229
  • 3
  • 13
  • 28
  • @Frank van Puffelen can you please see it – komal deep singh chahal Aug 04 '16 at 14:02
  • What I recall is when you call `push` it puts the unique key in the database for you, then you have to make a new child and put your data in there. I'm not sure though, I'm an Android programmer, so it might be different. And Frank won't get a notification, since if he didn't do anything on this post, he won't get notified – Ali Bdeir Aug 04 '16 at 14:15

6 Answers6

69

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

There are two ways to invoke push in 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.

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

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

}
Community
  • 1
  • 1
iOSGeek
  • 5,115
  • 9
  • 46
  • 74
36

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);
}
Community
  • 1
  • 1
Ronnie Royston
  • 16,778
  • 6
  • 77
  • 91
  • 5
    Took me a while to realise why my code wasn't working, then I noticed the `key;` in your wonderful answer - I was using `key();`, like in v2. – Brad Adams Oct 27 '16 at 18:52
  • how can we fetch these keys back. these are just alpha-numeric ids. How can we reference them to read back the data. – Aakash Dave Sep 13 '17 at 08:47
  • 3
    See [Getting the unique key generated by push()](https://firebase.google.com/docs/database/admin/save-data#getting-the-unique-key-generated-by-push) – Ronnie Royston Sep 13 '17 at 20:14
  • how is this comment not getting 100 more reputation? I spent 1 hour finding this solution! – Nicoara Talpes Apr 07 '18 at 16:45
6

You can get last inserted item id using Promise like this

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));
Azam Alvi
  • 6,918
  • 8
  • 62
  • 89
  • Superb, it worked. Thanks but the not get Key. this.addressRef.push(addressObj).then(res => { console.log("address key = " + res.key) ; }); – Anand_5050 Sep 14 '19 at 11:19
  • This should be the correct answer as .push() is different than .update(). With update the node will be deleted and created again, where with .push() you will add to the node. – christostsang Aug 11 '21 at 11:22
2

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

Anand_5050
  • 1,019
  • 2
  • 10
  • 23
0

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".

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

Type 'String' has no compatible call signatures

Roger A. Leite
  • 394
  • 2
  • 18
0

This worked for me:

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

See Here: Saving data with firebase.

Manohar Khadka
  • 2,186
  • 2
  • 18
  • 30