【发布时间】:2018-03-19 13:43:09
【问题描述】:
我在使用 AngularFire2 的 Angular 应用程序中有以下代码。
打字稿:
constructor(db: AngularFirestore) {
this.booksCollectionRef = db.collection<Book>('books');
this.books = this.booksCollectionRef.snapshotChanges().map(actions => {
return actions.map(action => {
const data = action.payload.doc.data() as Book;
const id = action.payload.doc.id;
return { id, ...data };
});
});
}
HTML:
<md-list>
<md-list-item *ngFor="let book of books | async">
<h4 md-line>{{book.name}}</h4>
</md-list-item>
</md-list>
此代码按预期检索和绑定数据(更新集合时删除项目),现在我想按给定列对集合进行排序。我尝试使用 firebase orderBy 子句,但我不知道如何将它与 snapShotChanges() 方法一起使用。
【问题讨论】:
-
你试过了吗:
this.books = this.booksCollectionRef.orderBy('name','desc').snapshotChanges().map(actions => -
@GalBracha 也不起作用。
Property 'orderBy' does not exist on type 'AngularFirestoreCollection<Book>' -
你试过TypeScript默认的
sort()函数吗?如果不是,请向我们提供您的this.books的结构。 -
@GhassenLouhaichi,还没有。那将是我的最后一个选择。但我试图在 'snapshotChanges()' 中完成这项工作,因为我可能需要类似地在 firestore 集合中使用一些其他功能。
-
我没有关注,如果您在调用
snapshotChanges()之后使用.map,您不能简单地通过.sort调用来对结果进行排序吗?
标签: angular typescript firebase angularfire2 google-cloud-firestore