Google Firestore - how to get document by multiple ids in one round trip?

前端 未结 11 1826
余生分开走
余生分开走 2020-11-21 22:49

I am wondering if it\'s possible to get multiple documents by list of ids in one round trip (network call) to the Firestore.

11条回答
  •  青春惊慌失措
    2020-11-21 23:24

    I hope this helps you, it works for me.

    getCartGoodsData(id) {
    
        const goodsIDs: string[] = [];
    
        return new Promise((resolve) => {
          this.fs.firestore.collection(`users/${id}/cart`).get()
            .then(querySnapshot => {
              querySnapshot.forEach(doc => {
                goodsIDs.push(doc.id);
              });
    
              const getDocs = goodsIDs.map((id: string) => {
                return this.fs.firestore.collection('goods').doc(id).get()
                  .then((docData) => {
                    return docData.data();
                  });
              });
    
              Promise.all(getDocs).then((goods: Goods[]) => {
                resolve(goods);
              });
            });
        });
      }
    

提交回复
热议问题