I am trying to write a function to geocode a zipcode, and return the lat and lng to an object, and then return this object. I am then trying to assign the value of this geocode function to a variable to create an object.
When I console log the object created in this function, I get an object returned in my terminal as expected:
const geo = (zip) => {
geocoder.geocode(zip, function(err, data) {
let latLng = {};
latLng.lat = data.results[0].geometry.location.lat;
latLng.lng = data.results[0].geometry.location.lng;
console.log(latLng); // Returns Object As Expected
});
};
But when I instead replace the console.log(latLng)
with return latLng
I get an undefined value when assigning this function to a variable like so:
let newObj = geo(zip);
console.log(newObj) // Returns Undefined
I am confused because the initial console.log
does show an object is being made in the geocode function, but when I use return
it is coming back as undefined
. I think it is an issue with how I am assigning it to a variable. I am misunderstanding something without doubt.