How to get firebase id

22,118

Solution 1

The call to push will return a Firebase reference. If you are using the Firebase 3 API, you can obtain the unique key of the pushed data from the reference's key property:

var pushedRef = firebase.database().ref('/customers').push({ email: email });
console.log(pushedRef.key);

The key for the pushed data is generated on the client - using a timestamp and random data - and is available immediately.

Solution 2

Calling push() will return a reference to the new data path, which you can use to get the value of its ID or set data to it.

The following code will result in the same data as the above example, but now we'll have access to the unique push ID that was generated:

// Generate a reference to a new location and add some data using push()
var newPostRef = postsRef.push();
// Get the unique ID generated by push()
var postID = newPostRef.key();

Documentation.

Share:
22,118
user3183411
Author by

user3183411

Updated on July 09, 2022

Comments

  • user3183411
    user3183411 almost 2 years

    Anyone know how to get the Firebase unique id? I've tried name(), name, key, key(). Nothing works.

    I am able to see the data but I have no idea how to get the id back. I need it.

    //Create new customers into firebase 
    function saveCustomer(email) {
    
      firebase.database().ref('/customers').push({
        email: email
      });
    
      firebase.database().ref('/customers').on("value", function(snapshot) {
        console.log(snapshot.val());
        console.log(snapshot.value.name());
      }, function(errorObject) {
        console.log("The read failed: " + errorObject.code);
      });
    
    
    }
    
  • cartant
    cartant almost 8 years
    You might want to mention that your answer is for the Firebase version 2 API.
  • user3183411
    user3183411 almost 8 years
    This method doesn't work. It states that key is not a function. Cartant's method worked.
  • FiringBlanks
    FiringBlanks about 7 years
    How would you find Firebase generated ID's that are already created? I can access the contents of the one I need and use them on my page as needed, but I need to know which ID they are all associated with. In this case, I don't need to create a new one.
  • cartant
    cartant about 7 years
    @FiringBlanks I think you should ask a question and include in it an example, as it's difficult to understand precisely what you mean. Reference this answer or question if necessary.
  • FiringBlanks
    FiringBlanks about 7 years
    @cartant Basically, how do you get an ID without creating a new one. Just figured it out. It's snapshot.getKey() to get the unique Firebase ID of the current snapshot. Drilling down to the snapshot first (snapshot.child('score').val();) and then running snapshot.getKey() on it will get you the key to the value of that javascript property (as in key/value pair). I think this is the answer @user3183411 was looking for.
  • srinivas
    srinivas over 3 years
    but this method won't work when you also need the id before - for example to save it in the data itself. Firebase suggests // Add a new document with a generated id. var newCityRef = db.collection("cities").doc(); --but in this case the reference contains the whole path. so need a different method for getting the id.
  • srinivas
    srinivas over 3 years
    see answer below for an alternate method which worked