I have the following code:
var myRootRef = new Firebase('https://myacct.firebaseIO.com');
var collectionRef = myRootRef.child('collection');
var pushRef = collectionRef.push();
pushRef.set('item1');
pushRef.set('item2');
pushRef.set('item3');
If I go to the url: ‘https://myacct.firebaseIO.com’, I see only ‘item3’ in collection. I do not see the other two. It appears that the collection object is being rewritten rather than being added to. If I manually type out the child names, things work. For example, the following works:
collectionRef.child(1).set('item1');
collectionRef.child(2).set('item2');
collectionRef.child(3).set('item3');
Is this a bug, or am I doing something wrong in the way I am using push?
Thanks.
The code you’ve written is working as expected. push() is a helper that creates a reference to a new child with a unique name, and it’s a good way to create a list of items in Firebase.
Your code could be rewritten as follows:
push() can optionally take an argument that will be set as the value at the new reference. You can therefore shorten your code a bit like this:
You can read more about working with push() here:
https://www.firebase.com/docs/managing-lists.html