My Code:
var latlng = {lat: 7, lng: 7};
function findLatLng() {
var testLocation = '350 Victoria St, Toronto';
axios.get('https://maps.googleapis.com/maps/api/geocode/json', {
params: {
address: testLocation,
key: 'MY KEY',
}
})
.then(function(response) {
console.log(response.data.results[0].geometry.location);
return response.data.results[0].geometry.location;
})
.catch(function(error) {
console.log(error);
});
}
latlng = findLatLng();
console.log(latlng);
In the console, latlng
is logged as undefined, but the console.log(response.data.results[0].geometry.location)
is logged with the proper value;
it is also logged afterwards even though it should have executed first.
I think this is because the code continues despite findLatLng()
not being done with the request but I'm not sure.
When I check the value of latlng
in the console afterwards, it becomes the proper value.
What is the issue and how do I fix this please?