How to get firebase id

Does anyone know how to get a unique Firebase ID? I tried name(), name, key, key(). Nothing works.

I can see the data, but I have no idea how to return the identifier. 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);
  });


}
+4
source share
2 answers

Call pushreturn Firebase link. If you use the Firebase 3 API, you can get a unique key from the clicked data from the property key:

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 immediately available.

+7
source

push() , .

, , push, :

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

.

+3

Source: https://habr.com/ru/post/1651198/


All Articles