【发布时间】:2019-06-01 21:36:03
【问题描述】:
我正在使用数据访问服务从 firebase firestore 获取数据。
如何使用 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
我希望 this.product 返回文档值和文档 ID
谢谢!!
【问题讨论】:
-
this.product的返回会包含 id。所以this.product.id是文档的id。要通过返回访问它,您必须使用this.service.getProduct().subscribe((product) => { console.log(product.id) }) -
@Swoox - 感谢您的回复,您的方法将起作用,但 snapshotChanges() 不起作用,错误属性 'map' 在类型 'Action
>' 上不存在。
标签: angular firebase google-cloud-firestore angularfire5