简体   繁体   English

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

[英]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.当我尝试从子集合中接收文档时,我得到一个 object,其中仅包含文档中的值。

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?有没有办法从整个文档中接收 object?

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.听起来您正在寻找documentChanges stream ,它(与valueChanges不同)在其 output 中包含每个文档的 ID。

I'd recommend checking out the AngularFire documentation on streaming collection data for the types of streams, and the differences between them.我建议查看关于流式收集数据的 AngularFire 文档,了解流的类型以及它们之间的区别。

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

相关问题 如何使用 Cloud firestore 中的文档 ID 访问文档字段? - How can I acess a document field using document id from cloud firestore? 如何使用帮助文档 ID 更新 Cloud firestore 文档字段? - How can i Update Cloud firestore document Field with the help DocumentID? 无法在云 Firestore 集合中添加多个文档 - Can't add more than one document in the cloud firestore collection 如何获取集合 Cloud Firestore 中的文档 ID 列表? - How to get a list of document IDs in a collection Cloud Firestore? Android:Paging3 使用 cloud-firestore 创建远程中介 - Android: Paging3 create remotemediator with cloud-firestore 如何从 firestore 数据库中获取嵌套集合 - How can I get the nested collection from firestore database 未创建文档的 Cloud Firestore 子集合 - Cloud Firestore Sub-Collection on a Document not Creating 如何检查 Cloud Firestore 中任何文档的集合中是否存在值(例如名称)? - How can I check if a value e.g. name exists in a collection within any of documents in Cloud Firestore? 无法从 flutter Cloud firestore 获取一份文件 - Can't get one document from flutter cloud firestore 如何检查 JS 中的 firestore(不是文档)中是否存在集合 - How do I check if collection exist in firestore (not document) in JS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM