I'm building a chatapp there
And i'm using Firebase Realtime Database and Firebase V9.1.1
How can i ordering the snapshot by the Date Key?
There is the look of my DB Structure
As we can see, the data inside DB isn't ordering the date by default
There is the query i tried:
onValue(query(ref(db, chatBaseURL), orderByChild('chatDate')), snapshot => console.log(snapshot.val())
The problem is that you call snapshot.val() on the entire result snapshot. This gets you all the results as a JSON object, and the keys in a JSON object have an undefined sort order by definition.
To log the results in the order you requested, loop over the resulting snapshot with snapshot.forEach():
onValue(query(ref(db, chatBaseURL), orderByChild('chatDate')), results =>
results.forEach((snapshot) => {
console.log(snapshot.val());
});
});