Sort firebase data in descending order using negative timestamp

后端 未结 1 1218
说谎
说谎 2020-12-06 21:03

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:

相关标签:
1条回答
  • 2020-12-06 21:48

    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);
    
    0 讨论(0)
提交回复
热议问题