Search code examples
javascriptarraysasync-awaitnuxt.jsapollo

asyncData with apollo query returns an object but not an array


I am using apollo to get my data in a Nuxt.js project, using asyncData

import homeQuery from '~/apollo/queries/home'

export default {
  async asyncData({app}) {
    const homeresult = await app.apolloProvider.defaultClient.query({
    query: homeQuery
  })
  return { home: homeresult.data.home }
},
data () {
  return {
    home: {}
  }
}

this works fine when the result of the query is an object, for example the above is:

 {
   "data": {
     "home": {
       title": "Home"
     }
   }
}

However, if the query result is an array:

{
  "data": {
    "home": [
      {
        "id": "1"
      },
      {
        "id": "2"
      },
      {
        "id": "3"
      }
    ]
  }
}

nothing gets returned. (I also tested data () { return { home: [] } } )

Do I have to treat arrays differently, and how should I correctly write the asyncData?


Solution

  • I have absolutely no idea what changed... but when I tried again that code, I had no more problems with arrays as results.