apollo icon indicating copy to clipboard operation
apollo copied to clipboard

Can't seem to initialize data

Open Christilut opened this issue 8 years ago • 12 comments

  data () {
    return {
      me: {
        name: null
      }
    }
  },

  apollo: {
    me: gql`{
        me {
          name
        }
      }`
  }

name remains null after the query. I can access the data but only with $data.$apolloData.data.me.name which seems like the wrong way.

If I remove the me {} property from data() then I get the following error:

TypeError: Cannot read property 'hasOwnProperty' of undefined
    at _loop (vue-apollo.esm.js?ebd5:3924)
    at VueComponent.launch (vue-apollo.esm.js?ebd5:3938)
    at callHook (vue.runtime.esm.js?ff9b:2917)
    at VueComponent.Vue._init (vue.runtime.esm.js?ff9b:4626)
    at new VueComponent (vue.runtime.esm.js?ff9b:4794)
    at createComponentInstanceForVnode (vue.runtime.esm.js?ff9b:4306)
    at init (vue.runtime.esm.js?ff9b:4127)
    at createComponent (vue.runtime.esm.js?ff9b:5604)
    at createElm (vue.runtime.esm.js?ff9b:5551)
    at VueComponent.patch [as __patch__] (vue.runtime.esm.js?ff9b:6087)

I just started this project with vue-cli 3 and added apollo with vue-cli. I've disabled subscriptions.

Christilut avatar May 07 '18 21:05 Christilut

This seems to be a regression of the latest version. Will fix it tomorrow. In the meantime, you can remove your data () { ... } option.

Akryum avatar May 07 '18 23:05 Akryum

Is there a short-term workaround for this issue?

maziarz avatar May 08 '18 20:05 maziarz

@maziarz

It seems like a simple work around, until the issue is fixed, is to employ the result callback and set your data manually.

export default {
  data () {
    return {
      me: {
        name: null
      }
    }
  },

  apollo: {
    me: {
      query: gql`{
        me {
          name
        }
      }`,
      result({ data, loading, networkStatus }) {
        this.me = data.me
      },

      error(err) {
        // handle your error
      }
    }
  }
}

At least, that's what I did, and it seems to be working so far.

Hope it helps,

~ Ayiga

Ayiga avatar May 09 '18 00:05 Ayiga

Thanks @Ayiga, that did the trick :-)

maziarz avatar May 09 '18 06:05 maziarz

Same issue here, @Akryum can you fix that?

pschaub avatar May 09 '18 10:05 pschaub

I don't think it's the correct way to use result to pass data in the vm but it's a good way before the fix. Maybe is better to downgrade to beta 5.

chatondearu avatar May 09 '18 15:05 chatondearu

Maybe is better to downgrade to beta 5.

This. 🐈

Akryum avatar May 09 '18 18:05 Akryum

Using for the first time (through nuxtjs/apollo) been trying to figure it out all day until i came across this.

Downgrading has fixed the issue for now.

binzcodes avatar May 09 '18 19:05 binzcodes

@binzcodes same here, as soon as there is a release, the nuxt module should be updated

maziarz avatar May 11 '18 13:05 maziarz

beta 10 is released already 🐈

Akryum avatar May 11 '18 14:05 Akryum

@Akryum excellent! Thanks 🥇

maziarz avatar May 11 '18 14:05 maziarz

great thanks @Akryum 👍

dohomi avatar May 13 '18 05:05 dohomi