![](/img/trans.png)
[英]How to get multiple random documents from angularfire firestore
[英]How to get particular document data with id ? | AngularFire 5.1.1 | Cloud Firestore | Documents
我正在使用數據訪問服務從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
謝謝!!
文檔只是一個對象{[field]: value}
而collection是文檔的容器[document]
你試圖獲得一個文件,即對象和問題,你不能直接映射它,我想你想得到整個集合,如果! 你可以知道所有文件的地圖
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))) }
抱歉英語不好
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.