Reputation: 3254
if (XMLData.product[i].image[0].thumb) {thumbURL = XMLData.product[i].image[0].thumb;}
Returns: TypeError: Error #1010: A term is undefined and has no properties.
Same with
if (XMLData.product[i].image[0].thumb!=undefined) {thumbURL = XMLData.product[i].image[0].thumb;}
How do I check if a variable exists?
Upvotes: 0
Views: 2270
Reputation:
PFhayes is right, you need to ensure each level of properties you're stepping through are defined. OR, if you want to be lazy, you can just write it in a try/catch statement. Like so:
if (XMLData.product[i] && XMLData.product[i].image[0] && XMLData.product[i].image[0].thumb)
{
thumbURL = XMLData.product[i].image[0].thumb;
}else{
//Not defined somewhere
}
or
try{
thumbURL = XMLData.product[i].image[0].thumb;
}catch(err:Error){
//Something went wrong. You can analyze the error data from here and act accordingly
}
Upvotes: 1
Reputation: 3927
The correct way is to compare it to undefined (though you should use !== instead of !=). It may be possible that it is an earlier object is undefined. To help debug this problem, you may need to check that
XMLData
XMLData.product
XMLData.product[i]
XMLData.product[i].image
XMLData.product[i].image[0]
XMLData.product[i].image[0].thumb
are all not equal to undefined.
Upvotes: 5