I am trying to get the documents inside an subcollection which is part of an document found with the .where function
Example:
- RootColl/
- Doc A/
- SubColl 1
- Doc 1
- Doc 2
- Doc 3
- SubColl 2
- Docs
- SubColl 1
- Doc A/
- SubColl 1
- Doc 1
- Doc 2
- Doc 3
- SubColl 2
- Docs
- SubColl 1
I want to get all the documents under SubColl 1
from the doc with the field level == 1
I am trying to do it like:
db.collection("RootColl").where("field", "==", "1").collection("SubColl 1").get()
But by doing that I get the error
Uncaught TypeError: db.collection(…).where(…).collection is not a function
EDIT 1: By following Frank van Puffelen suggestion, i get the same error, “collection” is not a function
Advertisement
Answer
A sub-collection lives under a specific document. A query as you’ve shared now points to a number of documents. You’ll need to execute the query to determine what documents it points to, then loop over the results, and get the sub-collection for each document.
In code:
var query = db.collection("RootColl").where("field", "==", "1"); query.get().then((querySnapshot) => { querySnapshot.forEach((document) => { document.ref.collection("SubColl 1").get().then((querySnapshot) => { ... }); }); });