使用负时间戳以降序对Firebase数据进行排序 [英] Sort firebase data in descending order using negative timestamp
问题描述
:
const feedRef = database.ref('ducks')
var feed = []; $($)$ b $ feedRef.orderByChild('timestamp')。on('value',(snapshot,error)=> {
snapshot.forEach((duckSnap)=> {
const duck = duckSnap.val()
console.log(duckSnap.key +'='+ duck.name);
feed.push(duck);
});
});
console.log(feed);
above is a screenshot of my firebase database. i am trying to sort firebase data in descending order using negative timestamp. I 'm using the following command:
const feedRef = database.ref('ducks')
feedRef.orderByChild('timestamp').on('value', (snapshot, error) => {
const feed = snapshot.val()})
i keep getting the feed in the same order as the database not in the descending order like i want.I think its not working because ducks endpoint doesnt have a child name timestamp how would i achieve this? Do the push generated keys have timestamp data ?
When you call .val()
on a snapshot, it gets converted into a JSON object. And the keys in a JavaScript object are by definition unordered (although many implementations will iterate the keys in lexicographical order).
If you run a query on the Firebase Database, you must ensure you don't convert it to JSON before you get the items in the right order. In your case, by using DataSnapshot.forEach()
:
const feedRef = database.ref('ducks')
var feed = [];
feedRef.orderByChild('timestamp').on('value', (snapshot, error) => {
snapshot.forEach((duckSnap) => {
const duck = duckSnap.val()
console.log(duckSnap.key+'='+duck.name);
feed.push(duck);
});
});
console.log(feed);
这篇关于使用负时间戳以降序对Firebase数据进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!