0

Im using parse.js for pulling data from database in SPA build in Quasar (based on vue.js). Im getting data undefined when fetching data from my database when calling the data through a method but it works when I put it straight into the create function. I guess they might be in different scopes (?) but I cannot figure out why.

This works:

export default {
  name: "MyPage",
  data() {
    return {
      myData: []
    };
  },

  created: async function() {
    let query = new parse.Query("TheData");
    const results = await query.find();
    this.myData = results;
    console.log(this.myData);
  }

But when I encapsulate the data into a method it returns

TypeError: Cannot set property 'myData' of undefined

export default {
  name: "MyPage",
  data() {
    return {
      myData: []
    };
  },

  created: async function() {
    this.getTheData();
  },
  methods: {
    getTheData: async () => {
      let query = new parse.Query("TheData");
      const results = await query.find();
      this.myData = results; // TypeError: Cannot set property 'myData' of undefined
      console.log(this.myData);
    },

1 Answer 1

1

I recommend that you try changing this

created: async function() {
  this.getTheData();
}

for

async mounted() {
    this.getTheData();
}

According to the documentation it says:

Don't use arrow functions on an options property or callback, such as created: () => console.log (this.a) or vm. $ watch ('a', newValue => this. myMethod ()). Since an arrow function doesn't have a this, this will be treated as any other variable and lexically looked up through parent scopes until found, often resulting in errors such as Uncaught TypeError: Cannot read property of undefined or Uncaught TypeError: this.myMethod is not a function.

https://v2.vuejs.org/v2/guide/instance.html#Instance-Lifecycle-Hooks

Sign up to request clarification or add additional context in comments.

Comments

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.