Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
100
rated 0 times [  101] [ 1]  / answers: 1 / hits: 26712  / 8 Years ago, sun, august 14, 2016, 12:00:00

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


}

More From » node.js

 Answers
69

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.


[#61043] Thursday, August 11, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
aidan

Total Points: 72
Total Questions: 95
Total Answers: 121

Location: Uzbekistan
Member since Sat, Feb 27, 2021
3 Years ago
aidan questions
Mon, Oct 11, 21, 00:00, 3 Years ago
Wed, Sep 29, 21, 00:00, 3 Years ago
Sun, Sep 5, 21, 00:00, 3 Years ago
Thu, Jan 16, 20, 00:00, 4 Years ago
;