I am calling two requests with axios.all()
on mounted()
and I can not assign the response data to my Vue data.
I have included some console.log()
in order to understand what is going on.
app = new Vue({
data: {
countries: [],
cities: [],
},
methods: {
getCountries() {
return axios({
method: 'get',
url: '/utils/countries/',
})
},
getCities() {
return axios({
method: 'get',
url: '/utils/cities/',
})
}
},
mounted() {
console.log('1 ' + this.cities)
axios.all([this.getCountries(), this.getCities()])
.then(axios.spread(function (countries, cities) {
this.countries = countries.data;
this.cities = cities.data;
console.log('2 ' + this.cities)
}))
.catch(error => console.log(error));
console.log('3 ' + this.cities)
}
});
Ouput :
1
3
2 [object Object]...
My call is successful but this.cities
and this.countries
remains empty.
How can I pass them my response data?