webduvet
webduvet

Reputation: 4292

How get autoID of new object after calling push() in firebase?

It's simple. I have an object called obj and the reference called ref when I do:

ref.push(obj, function(err){});

or

ref.push().set(obj, function(err){});

how do I get the auto generated ID of the recently saved object?

Upvotes: 2

Views: 1588

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598775

The push function returns a reference to the newly created object.

var newRef = ref.push();
console.log(newRef.name());
newRef.set(obj, function(err) {});

Note that in the above snippet no data is sent to the server until the call to set. The push() method is pure client-side, as long as you don't pass any data into it.

https://www.firebase.com/docs/web/api/firebase/push.html

Upvotes: 3

Related Questions