如何通过 Angular 应用程序检索 Firestore 文档数据?

How to Retrieve Firestore document data through Angular Application?

这里我需要检索文档数据和文档 ID 以显示相关的 post 数据。但是我没有找到更好的方法来做到这一点。请帮助我。

public postId: string;
public posts: any = [];    

this.postService.getRecentFivePosts().subscribe((res1) => {
                console.log(res1);
            });

getRecentFivePosts() {
        return this.firestore.collection('Post', ref => ref.orderBy('date', 'desc').limit(5)).snapshotChanges();
    }

您可以使用 Firestore 文档中的负载 属性 来获取数据,并使用 doc.id 属性 作为文档 ID。根据您的要求更改以下代码。

参考 - https://github.com/angular/angularfire/blob/master/docs/firestore/documents.md

this.postService.getRecentFivePosts().subscribe((res1) => {
                res1.map((k) => {
                    this.posts.push({
                        post: k.payload.doc.data(),
                        id: k.payload.doc.id
                    });
                });
            });