Nuxt - result.forEach is not a function?

3.4k views Asked by At

So i'm currently trying to pull data inside Nuxt from Cloud Firestore via:

async asyncData(){

    return firebase.firestore().collection('test').doc().get()
      .then((result) => {

        result.forEach(doc => {

          console.log(doc.data())          
        })       
      })
  }

which results in the following error:

async asyncData() {
    return _plugins_firebase_config__WEBPACK_IMPORTED_MODULE_0__["default"].firestore().collection('test').doc().get().then(result => {
      result.forEach(doc => {
        console.log(doc.data());
      });
    });
  }

with the error message:

TypeError
result.forEach is not a function

Pulling data without forEach works fine. So if i specify the doc by the name and get data only for one document, everything works as expected. However, since i need the whole collection of the data, i have to split up the data via forEach. Any ideas what could cause that? Same example works in VueJS itself just fine.

I've used nuxt cli to generate a new project, so the project setup should be fine.

1

There are 1 answers

0
Karolis Stakėnas On

First get and modify Your data, then return it.

Modify Your code so it looks like this:

async asyncData () {

    let data = []

    await firebase.firestore()
    .collection('test')
    .doc()
    .get()
    .then((result) => {
      result.forEach(doc => {

        data.push(doc.data())    

      })       
    })

    return data
}