I have the following function in my project that I access from several files.
getCountry: async function () {
let country = null
try {
axios
.get('http://ip-api.com/json/')
.then(response => (this.country = response.data.countryCode))
console.log('Country', country)
} catch (error) {
console.log(error)
}
return country
}
It works on the other files, but for some reason I cannot use it successfully in a particular file with this situation.
This is my data part:
data() {
return {
model: {
country: '',
state: '',
city: ''
},
country: null
}
},
And on mount part I am trying to do the following:
this.country = await this.getCountry()
if (this.country != null) {
this.model.country = this.country
}
The value never gets assigned to model.country. When I check this.country value, it gets the result from the function. I suppose it might have something to do with the fact that it's an async function, but I am not sure what to do.