Ad
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.
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
I want this.product returns the document value and document id
Thank You!!
Ad
Answer
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)))
}
Ad
source: stackoverflow.com
Related Questions
- → Make a Laravel collection into angular array (octobercms)
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → Angularjs not working inside laravel form
- → Analysis of Flux implementations
- → how to write react component to construct HTML DOM
- → angular ng-repeat and images in a row
- → Conditional BG Color on AngularJS
- → Should I 'use strict' for every single javascript function I write?
- → getting the correct record in Angular with a json feed and passed data
- → "Undefined is not a function" at .toBe fucntion
- → angularjs data binding issue
- → Angular / JavaScript auto hydrate an instance
- → Convert generic text string in json object into valid url using Angular
Ad