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.
JavaScript
x
17
17
1
//Create new customers into firebase
2
function saveCustomer(email) {
3
4
firebase.database().ref('/customers').push({
5
email: email
6
});
7
8
firebase.database().ref('/customers').on("value", function(snapshot) {
9
console.log(snapshot.val());
10
console.log(snapshot.value.name());
11
}, function(errorObject) {
12
console.log("The read failed: " + errorObject.code);
13
});
14
15
16
}
17
Advertisement
Answer
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:
JavaScript
1
3
1
var pushedRef = firebase.database().ref('/customers').push({ email: email });
2
console.log(pushedRef.key);
3
The key for the pushed data is generated on the client – using a timestamp and random data – and is available immediately.