John Abraham
John Abraham

Reputation: 18781

Get realtime updates with Cloud Firestore on entire collection

I tried following this SO but I'm still unable to log the data.

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

Upvotes: 2

Views: 963

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598718

When you listen to an entire collection, you get a 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. The document array is in a docs property under the query snapshot, so:

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

Upvotes: 4

Related Questions