1

I have the following apollo query setup. I want to pass the username dynamically but this.username is not recognized.

My code:

export default {
  data () {
    return {
      username: ''
    };
  },
  beforeCreate () {
    this.$Auth
      .currentAuthenticatedUser()
      .then((userProfile) => {
        this.username = userProfile.username;
      })
      .catch(() => this.$router.push({ name: 'auth' }));
  },
  apollo: {
    services: {
      query: gql(servicesByUser),
      variables: {
        username: this.username
      },
      update: (data) => {
        console.log('apollo:services', data);
        return data.servicesByUser.items;
      },
    },
  },
};

The below-hardcoded setup works:

variables: {
  username: "user-a"
},

This setup does not work:

variables: {
  username: this.username
},

Error:

TypeError: Cannot read property 'username' of undefined
    at Module../node_modules/babel-loader/lib/index.js?!./node_modules/@quasar/app/lib/webpack/loader.auto-import.js?

I have spent a few hours on it, still not able to figure the issue! Am I missing anything? Any help is much appreciated!

1 Answer 1

2

variables should be a function in order for this to be defined, as shown in the docs:

apollo: {
  services: {
    ...
    variables () {
      return {
        username: this.username,
      }
    },
  },
},
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, Daniel. I did wrap around function call as well prior to posting it here, maybe I missed something else. It works now and thanks so much!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.