I have defined a variable myData
outside a function and I try to assign the data which I get from axios to the variable myData
, but failed. The variable shareData
has the right value -- response.data
inside the function -- but the value of it turned to the original one when exiting the function.
It must be the local variable covers the outer one, so how do you avoid this and let the outer variable get the response.data
value?
var shareData = 'OUTSIDE THE FUNCTION'
axios({
method: 'get',
url: '/api/usershare', //API
responseType: 'json',
})
.then(function(response) {
shareData = response.data
console.log(shareData) //output the reponse.data correctly
})
console.log(shareData) //output OUTSIDE TEH FUNCTION, which is the orignal value of myData