2

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?

1
  • 2
    data['internal'] doesnt exist apparantly. need to check if does first if (typeof data['internal'] != 'undefined' && typeof data['internal']['new_insert_id'] != 'undefined') Commented Feb 27, 2013 at 12:47

6 Answers 6

4

Besides you can firstly check for existence of data['internal'], but you can also use the pythonic way, i.e. apply try/catch block:

try {
    var new_insert_id = data['internal']['new_insert_id'];
} catch (e) {}
Sign up to request clarification or add additional context in comments.

4 Comments

This is so elegant and simple. I've never written code like this, or heard of phythonic! It worked perfect.
@cosmicbdog Yeah, sometimes it can be perfectly handy.
I'm guessing it is 'trying' to do something, and 'catching' the error if there is one? I have to read up on this.
@cosmicbdog Exactly so, but in catch block we have nothing, so we just skip the problem. Such constructions are very popular in Python programming language, and can also be applied in JavaScript.
2

The statement you've written checks if new_insert_id property exists in 'internal', but it doesn't check if 'internal' exists in data variable. This should work better:

if(typeof data['internal'] != 'undefined' && typeof data['internal']['new_insert_id'] != 'undefined')
{
     var new_insert_id = data['internal']['new_insert_id'];
}

Comments

2

the error message says, that data['internal'] is already undefined. you need to check that before:

if(typeof(data['internal']) != 'undefined' && typeof data['internal']['new_insert_id'] != 'undefined')

Comments

2

You need to check data['internal'] !== undefined first :)

Comments

2

in your test, you are testing if the property ['new_insert_id'] of data['internal'] is undefined then you have trouble accessing it because data['internal'] is undefined hence the error you get.

You have first to check if data['internal'] is undefined.

Comments

2

I think it because data['internal'] is undefined.

So you need check data['internal'] first.

if(data['internal'] && data['internal']['new_insert_id'])
 {
     // if data['internal']['new_insert_id'] is defined, then..
     var new_insert_id = data['internal']['new_insert_id'];
 }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.