简体   繁体   English

使用Cloud Firestore获取整个馆藏的实时更新

[英]Get realtime updates with Cloud Firestore on entire collection

I tried following this SO but I'm still unable to log the data. 我尝试下面这个SO ,但我仍然无法登录数据。

Question: How do I unwrap the data of all docs of a collection? 问题:如何解开集合中所有文档的数据?

Snippet: 片段:

return await db.collection("articles")
    .onSnapshot((docs) => {
      console.log("Docs data: ", docs.map(doc => doc.data()); // wont work
    });

When you listen to an entire collection, you get a QuerySnapshot . 当您收听整个集合时,您将获得QuerySnapshot You need to loop over that to get the individual documents: 您需要遍历该循环以获取各个文档:

return await db.collection("articles")
    .onSnapshot((querySnapshot) => {
      querySnapshot.forEach((doc) => {
        console.log("Docs data: ", doc.data());
      })
    });

Update I now see that you're accessing docs as if it's an array. 更新现在,我看到您就像在访问数组一样访问docs The document array is in a docs property under the query snapshot, so: 文档数组位于查询快照下的docs属性中,因此:

return await db.collection("articles")
    .onSnapshot((querySnapshot) => {
      console.log("Docs data: ", querySnapshot.docs.map(doc => doc.data()); 
    })

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

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