Reputation: 25269
i'm loading in jquery a google map json and i receive correctly the json object called 'data'
sometimes — i can't identify exactly when or why and that's why i came here — even if i receive the regular values, BUT firebug console reads it is undefined!
for example, I have this line below in my js:
console.log(data.Placemark[i].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber);
in my Firebug Console i can see first the right value of the zipcode (10018), THEN i see this error:
data.Placemark[i].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode is undefined
i mean, if I get the value.. how could it be that is undefined??
here the complete code
$.ajax(
{
dataType: 'json',
url: "http://maps.google.com/maps/geo?q=" + myaddress + "&key=" + myapiKey + "&sensor=false&output=json&callback=?",
success: function (data, textStatus)
{
for (i = 0; i < data.Placemark.length; i++)
{
console.log(data.Placemark[i].AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber);
}
},
error: function (data)
{
/*nothing*/
}
});
I can add another detail: the error happens only when data.length is > 1 (basically when i get more than one address corresponding to my input)
Upvotes: 0
Views: 958
Reputation: 41401
I think you said it all. When the loop runs more than once (which is when the length is > 1), you're iterating through the second item, which doesn't have the property it trips up on. The first iteration does.
Upvotes: 1