[Vuejs]-Can't get value outside of Axios call

7👍

This is a problem with async, you are calling console.log before the axios request has finished. This is why we use the .then(res=>{}).

An alternative would be to use async await.

Decorate your parent function with async

const myFunction = async() => {
    const {data} = await axios.get('/api/get-user');
    this.user = data;
    console.log(this.user);
}

See MDN for more info, this link is to examples (which I find most helpful for understanding) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function#Examples

Leave a comment