[Vuejs]-How to get data from axios reponse outside the call function

0👍

Well, returns nothing because you are not assigning any value to the variable, you will have to assign the response data to a variable when the request fulfill:

latestVideo(videoID) {

  // var self = this;
  // var title;

  axios.get('http://video-api.dev/'+videoID)
  .then(response => {
    this.title = response.data.title
    console.log(response.data.title) //returns the correct title
  })
  // return title // not needed

}

Then, when the promise (the request) is resolved you can access to the variable from the component (this.title)

You don’t have to use the self switch if you are using arrow syntax.

👤Ander

0👍

You’re returning title, when assigning the response to this.title, try returning this.title.
I am making an assumption that latestVideo lives inside a class

class x {

latestVideo(videoID) {
    axios.get('http://video-api.dev/'+videoID)
    .then(response => {
        this.title = response.data.title
        console.log(response.data.title) //returns the correct title
    });

    return this.title
}}
👤CJBLOC

Leave a comment