简体   繁体   中英

How can I receive all document objects from a collection in cloud-firestore?

When I try to receive a document from a subcollection I get an object with only the values from the document.

The response looks like this:

{
 id: mealDocumentId,
 name: 'Burger',
 imageUrl: 'imgUrl1',
 price: 3, quantity: 1
} 

Is there a way to receive the object from the entire document?

I was expecting the objects from the documents similar to this:

 {
  mealDocumentId1: {
    name: 'Burger1',
    imageUrl: 'imgUrl1',
    price: 1,
    quantity: 1,
  },
  mealDocumentId2: {
    name: 'Burger2',
    imageUrl: 'imgUrl2',
    price: 2,
    quantity: 2,
  },
 }

The Code I'm running:

 constructor(private db: AngularFirestore) { }

  getAllMealDocuments() {
    // this is the path to the subselection meals
    const docRef = this.db.collection('tables').doc('documentId').collection('meals');
    
    // try to get a document object and pass it.
    return  docRef.snapshotChanges().pipe(map(document => {
      document.map(doc => {
        return new Meal({
          id: doc.payload.doc.id,
          ...(doc.payload.doc.data() as {})
        });
      });
    }));
  }

Is there another way to receive all the documents? Preferably like the expected formate? Thanks!

It sounds like you're looking for the documentChanges stream , which (unlike valueChanges ) includes the ID of each document in its output.

I'd recommend checking out the AngularFire documentation on streaming collection data for the types of streams, and the differences between them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM