Problem:
I’m trying to have all the documents in a collection printed out to me. However, the following code is not executed or I cannot get into the forEach loop. I have a nested collection in Firestore.
users//entries/uuid()/doc
const querySnapshot = await getDocs(collection(db, "eintraege"));
querySnapshot.forEach((doc) => {
// Never executed
console.log(doc.id, " => ", doc.data());
});
Solution:
The for loop is not executed because you’re using an incomplete reference. If you want to query the eintraege
subcollection, then you have to use the following reference:
const querySnapshot = await getDocs(collection(db, "users", "benj...@gmx.de" "eintraege"));
Where users
is the top-level collection, "benj...@gmx.de"
is the ID of the document, and eintraege
is the subcollection.