0

I have the following query to retrieve some data from firestore. However, when trying to push the data into classArray, it is empty. I researched a bit, it might be because I don't have await keyword, but not sure. Btw, the console.log does print the data, it's just not saving it to the array.

let classArray = []

firestore.collection('Classes')
        .doc(classType)
        .collection(subClass)
        .onSnapshot(snapshot => (
            snapshot.docs
            .map(doc => {
                classArray.push(doc.data());
                console.log(doc.data());
            }))
        )

How exactly do I fix this issue?

Danish Saeed
  • 49
  • 1
  • 5

1 Answers1

0

The .map() did the given conversion and returns the array; so you don't need to perform the push manually, just assigning it to your field is enough.

classArray  = snapshot.docs.map(doc => doc.data());
Muthu Thavamani
  • 1,325
  • 9
  • 19
  • Tried that and still the same issue. I think the await keyword might be the solution but I am not sure where I would put that – Danish Saeed Feb 03 '21 at 12:09
  • Sorry, I was able to get the issue from the snippet available in the question. If it's with async/await, that needs lot of explanation; but got a good ref for you - https://stackoverflow.com/questions/64521755/firestore-onsnapshot-or-async-await-issue-or-both – Muthu Thavamani Feb 03 '21 at 12:18