Skip to content

Getting a property of undefined #4

Open

Description

@littledan

What is the code triggering the message?

let z = { a: { b: { } } }
z.a.b.c.d

What went wrong in the code? What is the system trying to tell you?

That z.a.b.c is undefined, so you can't access the d property on it

What error message do you see?
Which JavaScript environment produced this error?

#### jsc
TypeError: undefined is not an object (evaluating 'z.a.b.c.d')

#### V8
TypeError: Cannot read property 'd' of undefined

#### chakracore
TypeError: Unable to get property 'd' of undefined or null reference

#### spidermonkey
TypeError: z.a.b.c is undefined

eshost shows that only SpiderMonkey gives helpful output

What error message would you like to see?

SpiderMonkey-style output in all JS engines

Do you agree to license your suggested error message under an MIT-style license?

Yes

Activity

  1. codehag commented on Oct 17, 2018

    @codehag
    Owner

    Playing around with elm style errors again.. just to see what they might look like:

    TypeError: z.a.b.c is undefined.
    
       --> components/some.js:511:26
    511 |        z.a.b.c.d
        |              ^^^
        |              |
        |              Property 'c' is undefined. Cannot access property 'd'. 
    
  2. brittanydionigi commented on Oct 18, 2018

    @brittanydionigi

    Another example related to this...

    class Calculator {
      constructor() {
        this.value = 0;
      }
    
      add(number) {
        this.value += number;
      }
    
      subtract(number) {
        this.value -= number;
        return this;
      }
    };
    
    let cal = new Calculator();
    cal.subtract(3).add(5).add(2);

    This code, which forgets to return this from the add method, gives you the following error:

    TypeError: Cannot read property 'add' of undefined

    I really like the Elm style pointers that tell you exactly there something is undefined, but in this case, when a method is returning undefined, it would be nice to be even more explicit and say something like The add() method of cal is returning undefined.

    I know that's probably a long shot, but A GIRL CAN DREAM.

  3. getify commented on Oct 31, 2018

    @getify

    I feel like the spidermonkey message is incomplete. I read that 4 times before I realized it was only talking about 'c' instead of 'd'. While it's correctly identifying the root problem, it doesn't mention the symptom at all and that seems surprising.

    I suggest:

    "TypeError: z.a.b.c.d could not be evaluated because z.a.b.c is undefined."

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions