Search code examples
javascriptecmascript-6google-geocoder

geoCoder respsone loop - Cannot read property '0' of undefined


I am sending a lon/lat to google geocoder and getting a response data that trying to break into variabels, the response look like this:

enter image description here

this is my code:

getFormattedAddress(result) {
    const arrResult = result.address_components;
    let itemRoute = '';
    let itemLocality = '';
    let itemCountry = '';
    let itemPc = '';
    let itemSnumber = '';

    for(let address_item in arrResult) {
        if (address_item.types[0] == "route") {
            console.log(i + ": route:" + address_item.long_name);
            itemRoute = address_item.long_name;
        }

        if (address_item.types[0] == "locality") {
            console.log("town:" + address_item.long_name);
            itemLocality = address_item.long_name;
        }

        if (address_item.types[0] == "country") {
            console.log("country:" + address_item.long_name);
            itemCountry = address_item.long_name;
        }

        if (address_item.types[0] == "postal_code_prefix") {
            console.log("pc:" + address_item.long_name);
            itemPc = address_item.long_name;
        }

        if (address_item.types[0] == "street_number") {
            console.log("street_number:" + address_item.long_name);
            itemSnumber = address_item.long_name;
        }
    }

}

The error that I am getting is: Uncaught (in promise) TypeError: Cannot read property '0' of undefined

What I am missing?

Update, json outprint:

[
    {
        "long_name": "29-31",
        "short_name": "29-31",
        "types": [
            "street_number"
        ]
    },
    {
        "long_name": "Götgatan",
        "short_name": "Götgatan",
        "types": [
            "route"
        ]
    },
    {
        "long_name": "Södermalm",
        "short_name": "Södermalm",
        "types": [
            "political",
            "sublocality",
            "sublocality_level_1"
        ]
    },
    {
        "long_name": "Stockholm",
        "short_name": "Stockholm",
        "types": [
            "locality",
            "political"
        ]
    },
    {
        "long_name": "Stockholm",
        "short_name": "Stockholm",
        "types": [
            "postal_town"
        ]
    },
    {
        "long_name": "Stockholms län",
        "short_name": "Stockholms län",
        "types": [
            "administrative_area_level_1",
            "political"
        ]
    },
    {
        "long_name": "Sverige",
        "short_name": "SE",
        "types": [
            "country",
            "political"
        ]
    },
    {
        "long_name": "118 26",
        "short_name": "118 26",
        "types": [
            "postal_code"
        ]
    }
]

Solution

  • var  arr = [
        {
            "long_name": "29-31", "short_name": "29-31",
            "types": [ "street_number" ]
        },
        {
            "long_name": "Götgatan", "short_name": "Götgatan",
            "types": [ "route" ]
        },
        {
            "long_name": "Södermalm", "short_name": "Södermalm",
            "types": [ "political", "sublocality", "sublocality_level_1" ]
        },
        {
            "long_name": "Stockholm", "short_name": "Stockholm",
            "types": [ "locality", "political" ]
        },
        {
            "long_name": "Stockholm", "short_name": "Stockholm",
            "types": [ "postal_town" ]
        },
        {
            "long_name": "Stockholms län", "short_name": "Stockholms län",
            "types": [ "administrative_area_level_1", "political" ]
        },
        {
            "long_name": "Sverige", "short_name": "SE",
            "types": [ "country", "political" ]
        },
        {
            "long_name": "118 26", "short_name": "118 26",
            "types": [ "postal_code" ]
        }
    ]
    
    for (var i=0; i< arr.length ; i++) {
        if (arr[i].types.indexOf('route') > -1)
            console.log( 'route ' + arr[i].long_name );
    
        if (arr[i].types.indexOf('locality') > -1)
            console.log( 'locality ' + arr[i].long_name );
    }