Home > Mobile >  Getting data from firebase cloud firestore
Getting data from firebase cloud firestore

Time:02-23

I have created a collection with

db.collection("userCal/" currentUserUID "/activities").add({event}) and now im trying to query all of this data from firebase but i dont think im doing it correctly.

it looks like this in firebase

enter image description here

and inside activities it looks like this:

enter image description here

I have this right not but its failing.

let doc = await fire
  .firestore()
  .collection('userCal')
  .doc(currentUserUID)
  .collection("activities")
  .get()

let dataObj = doc.data();
console.log(dataObj);

CodePudding user response:

When you use get() on a CollectionReference to fetch multiple documents from that collection, it returns a QuerySnapshot unlike using get() on a DocumentReference that just returns a DocumentSnapshot of a single document. So you'll have to run a loop on all documents present in the response as shown below:

const qSnap = await fire
  .firestore()
  .collection('userCal')
  .doc(currentUserUID)
  .collection("activities")
  .get()

const data = qSnap.docs.map(d => ({ id: d.id, ...d.data() }))
console.log(data);
  • Related