Skip to content
Advertisement

Is there a way to get all documents in a subcollection (Firestore)

I’m having difficulties querying all documents inside a subcollection in my Firestore database. (I am using Node.js)

My goal is to get all the data from every document inside the subcollection named favorites

My database structure looks like this. firestore db stucture

I looked at the documentation at https://firebase.google.com/docs/firestore/query-data/get-data but without any result how to solve the problem.

My query looks like this right now:

exports.getAllFavoritePodcasts = (req, res) => {
  db.collection('users')
    .doc(req.user.userId)
    .collection('favorites')
    .then(snapshot => {
      snapshot.forEach(doc => {
        console.log(doc.id, '=>', doc.data());
      });
    })
    .catch(err => {
      console.log('Error getting documents', err);
    });
}

But I get TypeError: db.collection(...).doc(...).collection(...).then is not a function

Advertisement

Answer

To summarize, get() method has to be called to retrieve the results. It can also be found in this example of getting all documents in a collection, in the Cloud Firebase article you have mentioned.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement