繁体   English   中英

如何从 cloud-firestore 中的集合接收所有文档对象?

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

当我尝试从子集合中接收文档时,我得到一个 object,其中仅包含文档中的值。

响应如下所示:

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

有没有办法从整个文档中接收 object?

我期待文档中的对象与此类似:

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

我正在运行的代码:

 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 {})
        });
      });
    }));
  }

是否有其他方式接收所有文件? 最好喜欢预期的甲酸盐? 谢谢!

听起来您正在寻找documentChanges stream ,它(与valueChanges不同)在其 output 中包含每个文档的 ID。

我建议查看关于流式收集数据的 AngularFire 文档,了解流的类型以及它们之间的区别。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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