Javascript firebase -> 日期顺序相反

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44442816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 02:31:45  来源:igfitidea点击:

firebase -> date order reverse

javascriptfirebasefirebase-realtime-database

提问by King in the world.

I am currently making an app using Firebase.

我目前正在使用 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

结果->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?

我如何订购 firebase 数据库的反向?

回答by Frank van Puffelen

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

Firebase 数据库将始终按升序返回结果。没有办法扭转它们。

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.
  1. 让数据库进行过滤,然后在客户端反转结果。
  2. 向数据库添加一个反向值,并将其用于查询。

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

这些选项之前已经讨论过很多次了。因此,与其重复,我将列出以前的答案:

回答by Suraj Kumar

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;
}

回答by Bryan Lim

If you want to display it in the front end, I suggest that after you retrieve the data, use the reverse()function of JavaScript.

如果要在前端展示,建议在检索到数据后,使用reverse()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();

回答by Danut Pralea

Far more easier is just use Swift's reversed(): https://developer.apple.com/documentation/swift/array/1690025-reversedhttps://developer.apple.com/documentation/swift/reversedcollection

使用 Swift 更容易reversed()https: //developer.apple.com/documentation/swift/array/1690025-reversed https://developer.apple.com/documentation/swift/reversedcollection

let decodedIds = try DTDecoder().decode([String].self, from: value)
// we reverse it, because we want most recent orders at the top
let reversedDecodedIds = decodedIds.reversed().map {
 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;
}

回答by Wolfyr

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

我是这样解决的:首先,我在我的服务中进行了一个查询,我以毫秒为单位按日期过滤:

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

}

}

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

然后为了首先获取最新日期,我将它添加到我的组件中,我从我的服务调用该方法:

posts.add(post);

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.

我把时间当作日期。由于较新的日期将是从 9999999999999 中扣除的较大数字,因此最新日期将首先出现在我的服务中的查询中。

Hope this solved it for you

希望这为您解决了

回答by kashlo

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

我已经结束更改我在前端部分创建列表的方式。

was

曾是

posts.insert(0, post);

changed to

变成

postRef.orderByChild('createData').orderByChild('createData').on('child_added',(data)=>{
console.log(data.val().name + data.val().createData);})

回答by Ruhban Shah

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

您可以使用一种方法来保存具有负值的相同或替代子项,然后对其进行解析。

##代码##