I am currently making an app using Firebase.
It is one of those bulletin boards that can be seen anywhere on the web.
But there was one problem.
This is a matter of date sorting.
I want to look at the recent date first, but I always see only the data I created first.
postRef.orderByChild('createData').startAt(reverseDate).limitToFirst(1).on('child_added',(data)=>{
console.log(data.val().name + data.val().createData);
})
result - >hello1496941142093
My code is the same as above.
How can I check my recent posts first?
How Do I order reverse of firebase database?
You can simply make a function to reverse the object and then traversing it.
Ive ended changing how I create my list on the frontend part.
was
changed to
You could use a method where you save the same or alternate child with a negative value and then parse it.
If you want to display it in the front end, I suggest that after you retrieve the data, use the
reverse()
function of JavaScript.Example:
This is how I solved it: First I made a query in my service where I filter by date in milliseconds:
}
Then to get the newest date first I added this to my component where I call the method from my service:
I pass the time let as the date. Since a newer date will be a bigger number to deduct from the 9999999999999, the newest date will turn up first in my query inside my service.
Hope this solved it for you
The Firebase Database will always return results in ascending order. There is no way to reverse them.
There are two common workaround for this:
These options have been covered quite a few times before. So instead of repeating, I'll give a list of previous answers: