Home > Article > Web Front-end > How to Display Firebase Posts in Descending Posted Order?
How to Display Posts in Descending Posted Order with Firebase
Firebase allows users to post comments using the push method. To display the retrieved data in chronological order, use the following method:
fbl.child('sell').limit(20).on("value", function(fbdata) { // handle data display here }
However, this code displays the data in order of oldest to newest. To reverse the order, Firebase offers two options:
Example Code:
To use the first option, modify the push() code:
var ref = new Firebase('https://your.firebaseio.com/sell'); var item = ref.push(); // Append an inverted timestamp to the post object var postObject = {...yourObject, timestamp: 0 - Date.now()}; item.setWithPriority(postObject, 0 - Date.now());
To use the second option, modify the retrieval code:
fbl.child('sell').startAt().limitToLast(20).on('child_added', function(fbdata) { console.log(fbdata.exportVal()); })
Notes:
The above is the detailed content of How to Display Firebase Posts in Descending Posted Order?. For more information, please follow other related articles on the PHP Chinese website!