0

How can i fetch the return value using this code.

axios.get('/task')
    .then(response => {
        this.tasks = response.data.tasks;
        return this.tasks;
});

It is a possible with this code or is any other means on how I can retrieved the value. I cannot retrieved tha value using the return.

Thanks in advance.

3
  • What are you trying to achieve by returning the value like this? Commented Jan 22, 2018 at 5:09
  • To create the table for Vue.js together with the search and pagination functionality. Commented Jan 22, 2018 at 5:13
  • Please read the documentation on Promise objects. developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. Commented Jan 22, 2018 at 6:56

2 Answers 2

1

I suggest you to read about Vue's principle concepts such as its data reactivity system: https://v2.vuejs.org/v2/guide/reactivity.html

By setting local state like you do (this.tasks = response.data.tasks;), view elements react to data changes automagically and render accordingly.

For example, if you have a bunch of tasks that should be rendered, you could have something like this:

<template>
  <div class="tasks">
    <div v-for="task in tasks" class="task">{{ task }}<div>
  </div>
</template>

Whenever the HTTP request finishes and you set this.tasks = response.data.tasks, all of the tasks are automatically rendered. To answer your question: there is really no need to return after the request completes.

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

Comments

0

Have you tried like this?

let base = this;
axios.get('/task')
    .then(response => {
        base.tasks = response.data.tasks;
        return base.tasks;
});

5 Comments

Yes. but still the same. Value does not return using the above code
are you sure that response.data.tasks is returning you value?
Here you are using Arrow Syntax that's why your 'this' keyword getting conflict but I used base variable to get parent 'this' and I returned it. thing is make sure that you get value in response.data.tasks
Yes response.data.tasks. returns a value but when i use the code return this.data.tasks. And call the function. Its returns undefined. function getTaskData() { let base = this; axios.get('/task') .then(response => { base.tasks = response.data.tasks; return response.data.tasks; });
you can hold it in one variable and then try to return variable

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.