Reputation: 150
I am new to Firebase.I have a carousel which runs in sync with firebase.I have a small node script which adds a new item into my firebase ref.I dont know for some reason the node I try to insert is getting added in the middle rather than at the last.I do not know if I am doing something wrong.Any help would be useful for me.Here is the part of my node script which inserts node into my ref
var trendRef=firebase.database.ref("my-ref");
trendRef.child("OTR-124").update({<myObj>});
Note:I am adding a timeStamp field as a part of my object which takes classic javascript timestamp as value(this is the part which gives me more doubt in my mind)
Upvotes: 0
Views: 37
Reputation: 598728
The Firebase Database defaults to ordering data lexicographically by key. So in your case if you already had children NPR-431
and PYT-987
, your new child would end up in between them:
NPR-431
OTR-124
PYT-987
If your data contains a property that you want to order on, you can use Firebase queries to return the items in the order you want:
trendRef.orderByChild("propertyOnWhichToOrder").on(...
But if you want the keys to be ordered chronologically, you need to ensure that the key values are both chronological and lexicographical increasing. That sounds more difficult than it is, because Firebase has a built-in method that generates such keys for appending items to a list.
trendRed.push({<myObj>});
This will append the item to the end of the list with an auto-generated key that is guaranteed to be both ordered and unique.
Upvotes: 1