[英]How to get particular document data with id ? | AngularFire 5.1.1 | Cloud Firestore | Documents
I am using Data access service to get the data from firebase firestore. 我正在使用数据访问服务从firebase firestore获取数据。
How to use snapshotChanges()method for getting particular document data with id 如何使用snapshotChanges()方法获取具有id的特定文档数据
getProduct(id: number): Observable<Product> {
this.productsDocuments = this.angularfirestore.doc<Product>('products/' + id);
this.product = this.productsDocuments.snapshotChanges().pipe(
map(changes => changes.map(a => {
const data = a.payload.doc.data() as Product;
const id = a.payload.doc.id;
return { id, ...data };
}))
);
return this.product
I want this.product returns the document value and document id 我想this.product返回文档值和文档ID
Thank You!! 谢谢!!
a document is simply an object {[field]: value}
and collection is a container for documents [document]
文档只是一个对象{[field]: value}
而collection是文档的容器[document]
you are trying to get a single document aka object and the problem here that you cannot map it directly, i think that you wanna get the entire collection, if! 你试图获得一个文件,即对象和问题,你不能直接映射它,我想你想得到整个集合,如果! you can know map over all 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 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))) }
sorry for bad english 抱歉英语不好
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.