0

I need to get data from a subcollection. I have a collection products, it contains documents with a subcollection reviews.

products/prodID/reviews/reviewsID

My interfaces:

interface IReview {
  author: string;
  text: string;
  rating: number;
}

export interface IProduct {
  id: string;
  name: string;
  review:IReview[]
}

Function for retrieve data from db

getProductsFromDB():Observable<IProduct[]>{
  return this.db.collection('products').snapshotChanges()
    .pipe(
      map(actions => {
        return actions.map(a => {
          let data = a.payload.doc.data() as IProduct;
          data.id = a.payload.doc.id;
          return data;
        });
      })
    );
}

The problem is that now this function returns only the products collection data. But I need the subcollection data to be returned too.

  • 1
    Does this answer your question? [Firestore get all docs and subcollections of root collection](https://stackoverflow.com/questions/46875966/firestore-get-all-docs-and-subcollections-of-root-collection) – tornadoradon Mar 16 '23 at 14:58
  • Not really, because I work with observable. Most likely I do not understand something and do not know how to adapt your example for my task. For example, the get() function does not work for me – Илья Никитин Mar 16 '23 at 15:21

1 Answers1

0

You can read your subcollection by specifying the subcollection path from Firestore. See sample code below:

getProductsFromDB():Observable<IProduct[]>{
  return this.db.collection('products/{prodID}/reviews').snapshotChanges()
    .pipe(
      map(actions => {
        return actions.map(a => {
          let data = a.payload.doc.data() as IProduct;
          data.id = a.payload.doc.id;
          return data;
        });
      })
    );
}
Marc Anthony B
  • 3,635
  • 2
  • 4
  • 19