如何获取具有id的特定文档数据? | AngularFire 5.1.1 | Cloud Firestore |文件资料 [英] How to get particular document data with id ? | AngularFire 5.1.1 | Cloud Firestore | Documents

查看:81
本文介绍了如何获取具有id的特定文档数据? | AngularFire 5.1.1 | Cloud Firestore |文件资料的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用数据访问服务从Firebase Firestore获取数据.

I am using Data access service to get the data from firebase firestore.

如何使用snapshotChanges()方法获取具有ID的特定文档数据

How to use snapshotChanges()method for getting particular document data with 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

我要this.product返回文档值和文档ID

I want this.product returns the document value and document id

谢谢!

推荐答案

文档只是对象{[field]: value} 集合是文档[document]的容器.

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 }; })) }

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 }; })) }

用于收藏

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))) }

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))) }

这篇关于如何获取具有id的特定文档数据? | AngularFire 5.1.1 | Cloud Firestore |文件资料的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆