Vue-apollo: this in updateQuery

Created on 24 Nov 2017  路  8Comments  路  Source: vuejs/vue-apollo

I want to access some properties of the component in updateQuery, but when I'm calliing this in updateQuery of Subscription, the this is referring to the object itself. How can I access the component this in the updateQuery?

subscribeToMore: {
  variables () {
    console.log('variables.this', this) // it's correct, vue component
    return {}
  },
  updateQuery (result, { subscriptionData }) {
    console.log('updateQuery.this', this) // it's wrong, just the object itself, I guess missing binding this
  }
}
bug

Most helpful comment

@Samuell1 the this just access the https://github.com/Akryum/devfest-nantes-2017/blob/apollo2/app/src/components/PageHome.vue#L87 object. but not the vm.
but it works fine in variables. It's a bug (missing binding this for the function updateQuery) and I have fixed it in my project. If anyone has the same issue (seems you all don't think it's a bug), I will create a PR.

All 8 comments

You have to use arrow func like in the example:
updateQuery: (previousResult, { subscriptionData }) => { // Here, return the new result from the previous with the new data },

@duyduc-nguyen it can't because the arrow function this is just pointing to the outside object instead of the vm instance.

@Samuell1 the this just access the https://github.com/Akryum/devfest-nantes-2017/blob/apollo2/app/src/components/PageHome.vue#L87 object. but not the vm.
but it works fine in variables. It's a bug (missing binding this for the function updateQuery) and I have fixed it in my project. If anyone has the same issue (seems you all don't think it's a bug), I will create a PR.

@Akryum
how did you fix it sir?

Has this been fixed? Still cannot access this, which shows as undefined inside updateQuery. (3.0.0-beta.29)

EDIT: It works, looks like you cannot use arrow function
updateQuery (result, { subscriptionData }) { works but not updateQuery: (result, { subscriptionData }) => {

Same for me.

I can confirm that using arrow function does not work (this will be undefined) but using a function does work updateQuery (result, { subscriptionData })

One part of the problem is that the docs show an arrow function, which leads many to this error. A quick fix would be to update the docs to change the example, so copy-paste is safer 馃槅

Was this page helpful?
0 / 5 - 0 ratings

Related issues

alx13 picture alx13  路  4Comments

igaloly picture igaloly  路  3Comments

jsrkstr picture jsrkstr  路  3Comments

mathe42 picture mathe42  路  4Comments

sadhakbj picture sadhakbj  路  3Comments