3

So i've read a handful of SO posts and some blogs, but still can't figure out why my code isn't working.

My code:

function myFunct(d) {
    if (typeof d.parent.name == "undefined") {
        console.log("undefined") ;} else { console.log("defined") ;}
    }

d is an object that looks something like:

Object { 
children: Object, 
count: 676
}

I've tried using (!d.parent.name), hasOwnProperty, ===, and as above using typeof. Any suggestions?

The error I recieve is TypeError: d.parent.name is undefined

UPDATE:

Ok thanks everyone for the input, and my apologies if the question was confusing. I was actually looking for d.parent.parent but tried to simplify the question by using d.parent. I think the problem is that d.parent is not defined so it doesn't even get to d.parent.parent. Sorry for not being more specific!

17
  • @ForceMagic I receive the same error. Commented Apr 11, 2016 at 21:15
  • 1
    typeof Object === 'undefined' is a valid way to use typeof Commented Apr 11, 2016 at 21:17
  • @NickSlash sry, my bad :) Commented Apr 11, 2016 at 21:18
  • @NickSlash Thanks, unfortunately I still get the same TypeError Commented Apr 11, 2016 at 21:18
  • can you give this a try? if (d.parent && typeof d.parent.name === 'undefined') Commented Apr 11, 2016 at 21:20

4 Answers 4

5

If you want an undefined-safe check all the way down your object tree, you can use:

if( typeof( ((d || {}).parent || {}).name ) === 'undefined') {

}

If you have the luxury of having Lodash at your disposal:

var d = { 
     parent: {
          name: "Joe"
     }
};

if ( typeof (_.get(d, "parent.name")) === 'undefined' ) {

}
Sign up to request clarification or add additional context in comments.

Comments

4

Try to check all children with logical OR

if (typeof d == "undefined" ||
    typeof d.parent == "undefined" ||
    typeof d.parent.name == "undefined") {
// ...
}

2 Comments

Gah I get the same error... I must really be doing something wrong. Thanks though for the suggestions!
Append typeof d == "undefined" || to the beginning of the if condition
1
if(typeof x === 'undefined')

Use this, it checks for type as well as value, thats what you need.

5 Comments

Thanks for the input, however I still receive the same error
Are you sure you're referencing to an existing value?
Ya @As3adTintin, if for some reason d.parent itself is undefined, the script will error out before it even gets to checking d.parent.name, be aware of that
True, why don't you first check if the parent is undefined?
@jlane09 thanks for the feedback. I receive the same error weather I check for d.parent or d.parent.name
0

I believe the error is the property identifier parent. Are you sure your object has the property? The identifier d may be invalid because parent doesn't exists.

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.