I'm processing some return JSON data.
Sometimes the JSON will return something I can access via
var new_insert_id = data['internal']['new_insert_id'];
But sometimes this part of the json array data will not be returned at all, and so I need to skip this variable being set.
So I've written a simple check to make sure this data exists before trying to set the variable:
if(typeof data['internal']['new_insert_id'] != 'undefined')
{
// if data['internal']['new_insert_id'] is defined, then..
var new_insert_id = data['internal']['new_insert_id'];
}
But when the JSON returns and there is no new_insert_id I am getting the following error:
Uncaught TypeError: Cannot read property 'new_insert_id' of undefined
And the line of code it points to as the culprit is the line of my if statement.
What am I missing? I thought my if statement would check if it exists or not, or do I need to do something else when working with arrays?
data['internal']doesnt exist apparantly. need to check if does firstif (typeof data['internal'] != 'undefined' && typeof data['internal']['new_insert_id'] != 'undefined')