Gökhan Geyik
Gökhan Geyik

Reputation: 313

Not Retrieved React Native Firestore Subcollection

I have a problem when i want to get subcollections. Can anybody help me, please ?

firestore structure :

questions(collection) ->  curretUser.id(doc) -> questions(collection) -> questionIDautoCreated(doc)-> {title: "", description: "" }

And i try to get data:

return (dispatch) => {
  firebase.firestore().collection('questions').doc(currentUser.uid)
  .collection('questions').get()
  .then((documentSnapshot) => {
    const value = documentSnapshot.data();
    console.log('Value Succeed : ');
    console.log(value);
  });
};

Error :

Possible Unhandled Promise Rejection (id:0) Type Error: documentSnapshot.data is not a function

Thank You

Upvotes: 1

Views: 1689

Answers (2)

Cagatay
Cagatay

Reputation: 21

You can get subcollections like this,

Collections returns docs array, you must access docs data first. Quick Example

firebase.firestore().collection('...').get().then(s => console.log(s.docs))

Your solution

return (dispatch) => {
    firebase.firestore().collection(`questions/${currentUser.id}/questions`)
      .get()
      .then(questionSnapshot => questionSnapshot.docs)
      .then(questions => {
        questions.forEach(question => {
          console.log(question)
        })
      })
  }

Upvotes: 2

parohy
parohy

Reputation: 2180

Because collection is not a DocumentSnapshot, its a QuerySnapshot. Collection contains a list of data, it is not an actual document. If you look in the firestore docs here. You can see when querying for collection, you should:

querySnapshot.forEach(function(doc) {
    console.log(doc.id, " => ", doc.data());
});

The exception speaks for it self, the object you receive is not the one you expect.

return (dispatch) => {
  firebase.firestore().collection('questions').doc(currentUser.uid)
   .collection('questions').get()
   .then((querySnapshot) => {
     querySnapshot.forEach((doc) => {
       console.log(doc.id, " => ", doc.data());
     });
   });
 };

Upvotes: 3

Related Questions