在firebase和Ionic 2上降序orderByChild()

nfo*_*ont 3 sorting firebase ionic-framework firebase-realtime-database ionic2

我需要按日期排序项目,但显然我需要降序排序以正确的顺序显示帖子...

import {
  AngularFireDatabase
} from 'angularfire2/database';
import 'rxjs/add/operator/map';

/*
  Generated class for the FirebaseProvider provider.

  See https://angular.io/docs/ts/latest/guide/dependency-injection.html
  for more info on providers and Angular 2 DI.
*/
@Injectable()
export class FirebaseProvider {

  constructor(public afd: AngularFireDatabase) {}


  getPostsItems() {
    return this.afd.list('/Posts/', {
      query: {
        orderByChild: "date",
      }
    });

  }
Run Code Online (Sandbox Code Playgroud)

此查询返回一个上升顺序,我需要一个后代订单,它没有在Firebase Web中解释.

我需要哪些查询?

Wil*_*ill 5

一种方法可能是颠倒组件模板中的顺序.首先,您可以直接在组件中获得帖子列表:

posts.component.ts

export class PostsComponent {
  posts: FirebaseListObservable<any>;

  constructor(db: AngularFireDatabase) {
    this.posts = db.list('/posts', {
      query: {
        orderByChild: 'date'
      }
    });
  }
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以使用该reverse方法撤消模板中帖子的顺序:

posts.component.html

<div *ngFor="let post of (posts | async)?.slice().reverse()">
  <h1>{{ post.title }}</h1>
</div>
Run Code Online (Sandbox Code Playgroud)