Reputation: 153
I'm wondering how i am able to copy the unique ID that will be generated from PUSH to the firebase database to its Child as ID object .
see image example below:
My first approach was to generate my own uniqueID, however, i believe firebase's unique ID is more reliable than creating my own.
this.afd.database.ref().child('Data/'+ MyUniqueID).set(data);
Can you Advise? Thanks
Upvotes: 1
Views: 323
Reputation: 317372
When you call push()
with no arguments, it will return a database reference at the location of the push. It has a key
property with the unique key generated on the client. You can then use that key in the object that you then write at that location. For example, in plain javascript (not angular):
var objectToPush = { data: "foo" };
var ref = parent.push();
objectToPush.id = ref.key; // add the id to the object to write
ref.set(objectToPush);
Upvotes: 2
Reputation: 18555
See Update specific fields for web
// Get a key for a new Post. var newPostKey = firebase.database().ref().child('posts').push().key;
and Getting the unique key generated by push() for nodejs
// Generate a reference to a new location and add some data using push() var newPostRef = postsRef.push(); // Get the unique key generated by push() var postId = newPostRef.key;
Upvotes: 2