How to get particular document data with id ? | AngularFire 5.1.1 | Cloud Firestore | Documents

人盡茶涼 提交于 2019-12-06 11:44:06
Ezzabuzaid

A document is simply an object {[field]: value} and a collection is a container for documents [document].

You are trying to get a single document/object and the problem is that you cannot map to it directly. I think that you want to get the entire collection, and then map over all of the documents.

getProduct(id: number): Observable<Product> { const productsDocuments = this.db.doc<Product>('products/' + id); return productsDocuments.snapshotChanges() .pipe( map(changes => { const data = changes.payload.data(); const id = changes.payload.id; return { id, ...data }; })) }

for a collection

getProduct(id: string): Observable<Product[]> { const productsDocuments = this.db.collection<Product[]>('products'); return productsDocuments.snapshotChanges() .pipe( map(changes => changes.map(({ payload: { doc } }) => { const data = doc.data(); const id = doc.id return { id, ...data }; })), map((products) => products.find(doc => doc.id === id))) }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!