0

This is the sample code from Github. I am trying to geocode a user-submitted address and extract more information from by creating a function geocodeAddress (see the second block).

// Geocode an address.
googleMapsClient.geocode({
  address: '1600 Amphitheatre Parkway, Mountain View, CA'
}, function(err, response) {
  if (!err) {
    console.log(response.json.results);
  }
});

When I console.log the object it works but returns undefined. I have tried to initialize my object globally without success.

exports.geocodeAddress = function (address) {
    var newGeocodedAddress = {};
    googleMapsClient.geocode({ address: address }, function(err, response) {
        if (!err) {
            newGeocodedAddress.lat_lng = [response.json.results[0].geometry.location.lat, response.json.results[0].geometry.location.lng];
            newGeocodedAddress.formatted_address = response.json.results[0].formatted_address;
            newGeocodedAddress.place_id = response.json.results[0].place_id;
            var address_components = response.json.results[0].address_components;
            for (var i = 0; i < address_components.length; i++){
                if ('locality' === address_components[i].types[0]) {
                    newGeocodedAddress.city_name = address_components[i].long_name;
                }
                if ('administrative_area_level_1' === address_components[i].types[0]) {
                    newGeocodedAddress.province = address_components[i].short_name;
                }
                if ('country' === address_components[i].types[0]) {
                    newGeocodedAddress.country_code = address_components[i].short_name;
                }
                if ('postal_code' === address_components[i].types[0]){
                    newGeocodedAddress.postal_code = address_components[i].long_name;
                }
            }
        }
        // console.log(newGeocodedAddress) returns the expected value
        return newGeocodedAddress; // returns undefined
    });
};

strong text

Miguel Kouam
  • 21
  • 1
  • 4
  • https://stackoverflow.com/q/14220321/1169798 – Sirko Oct 30 '17 at 21:49
  • I'm confused by your statement "When I console.log the object it works but returns undefined." Elsewhere in the post you say the console.log returns the expected value. Can you please clarify? – nurdyguy Oct 30 '17 at 21:52
  • When I console.log(newGeocodedAddress) I see the data I am looking for, however, I would like to return that data so I can use it somewhere else in my program. When I run: return newGeocodedAddress; I get undefined. – Miguel Kouam Oct 30 '17 at 22:50
  • what you are getting here is expected. `googleMapsClient.geocode` is running asynchronously - so as soon as you hit `googleMapsClient.geocode` function - you just skip to the end of `exports.geocodeAddress` - and so the return value for `exports.geocodeAddress` is undefined. To solve this you have 2 options. If `googleMapsClient.geocode` returns a promise you just do: `return googleMapsClient.geocode .. ` if not - you need to create your own promise object inside that function and return that, and resolve it in callback. More about promises here: https://www.youtube.com/watch?v=s6SH72uAn3Q – AIon Oct 31 '17 at 07:50

0 Answers0