繁体   English   中英

使用AngularFirestore Collection orderBy和snapShotChanges方法

[英]Using AngularFirestore Collection orderBy with snapShotChanges method

我在使用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.booksCollectionRef = db.collection<Book>('books', ref => ref.orderBy('order field'));

有关此主题的更多信息,请查看AngularFirestore文档

this.announcementCollectionRef = afs.collection<Announcement>('announcements', ref => ref.orderBy('createdAt', 'desc')); this.announcements = this.announcementCollectionRef.snapshotChanges().map(actions => { return actions.map(a => { const data = a.payload.doc.data() as AnnouncementId; const id = a.payload.doc.id; return { id, ...data }; }); });

不确定Angular Fire,但您可以尝试直接使用Firebase Firestore库。

以下代码适用于我:

someCollectionRef
.orderBy('columnName')
.onSnapshot((snapshot) => {
snapshot.docChanges.forEach(function (change) {
...doStuff

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM