Search code examples
javascriptgoogle-mapsgoogle-maps-api-3google-maps-api-2

How to handle TypeError: place.address_components[2] in google autocomplete map search?


I am developing a google map app with autocomplete option. I am using place.address_components[2].short_name to fetch state. It works fine for some city but not for all. Like for arizona it gives TypeError: place.address_components[2] is undefined How to handle this error ?


Solution

  •     if (typeof  place.address_components[2] != 'undefined') {
                 var state = place.address_components[2].short_name.toLowerCase();
        } else {
                // do something else;
        }
    

    Above code solves my problem. Thanks @Spencer Lockhart for your answer.