firebase -> date order reverse

2020-01-24 05:52发布

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 firebase treeenter image description here

My code is the same as above.

How can I check my recent posts first?

How Do I order reverse of firebase database?

6条回答
够拽才男人
2楼-- · 2020-01-24 06:10

You can simply make a function to reverse the object and then traversing it.

function reverseObject(object) {
    var newObject = {};
    var keys = [];

    for (var key in object) {
        keys.push(key);
    }

    for (var i = keys.length - 1; i >= 0; i--) {
        var value = object[keys[i]];
        newObject[keys[i]]= value;
    }       

    return newObject;
}
查看更多
看我几分像从前
3楼-- · 2020-01-24 06:15

Ive ended changing how I create my list on the frontend part.

was

posts.add(post);

changed to

posts.insert(0, post);
查看更多
Summer. ? 凉城
4楼-- · 2020-01-24 06:19

You could use a method where you save the same or alternate child with a negative value and then parse it.

postRef.orderByChild('createData').orderByChild('createData').on('child_added',(data)=>{
console.log(data.val().name + data.val().createData);})
查看更多
叛逆
5楼-- · 2020-01-24 06:22

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:

let result = postRef
  .orderByChild("createData")
  .startAt(reverseDate)
  .limitToFirst(1)
  .on("child_added", data => {
    console.log(data.val().name + data.val().createData);
  });

result.reverse();
查看更多
叼着烟拽天下
6楼-- · 2020-01-24 06:23

This is how I solved it: First I made a query in my service where I filter by date in milliseconds:

 getImages (): Observable<Image[]> {
this.imageCollection = this.asf.collection<Image>('/images', ref => ref.orderBy('time').startAt(1528445969388).endAt(9999999999999));
this.images = this.imageCollection.snapshotChanges().pipe(
  map(actions => actions.map(a => {
    const data = a.payload.doc.data() as Image;
    const id = a.payload.doc.id;
    return { id, ...data };
  }))
);
return this.images;

}

Then to get the newest date first I added this to my component where I call the method from my service:

let date = new Date;
let time = 9999999999999 - date.getTime();
console.log(time);

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

查看更多
Anthone
7楼-- · 2020-01-24 06:29

The Firebase Database will always return results in ascending order. There is no way to reverse them.

There are two common workaround for this:

  1. Let the database do the filtering, but then reverse the results client-side.
  2. Add an inverted value to the database, and use that for querying.

These options have been covered quite a few times before. So instead of repeating, I'll give a list of previous answers:

查看更多
登录 后发表回答