1

I keep getting the error: "TypeError: Cannot read property 'push' of undefined"

The error is in this line: "this.courseName.push(dish);"

Here is the code:

let menu = {
    _courses: {
      _appetizers:[] ,
      _mains:[],
      _desserts: []
    },

    addDishToCourse(courseName, dishName, dishPrice){
        const dish = { name: this.dishName, price: this.dishPrice };

        // ERROR HERE: **this._courses[courseName].push(dish);**
    },
};

I have been stuck with this error for hours. Is there something I'm not seeing?

Sorry if there is a simple solution to this. I'm relatively new to JavaScript.

Edit:

I was indeed calling the key incorrectly. I am now calling the function as:

  menu.addDishToCourse("_appetizers", "random", 10);
  console.log(menu._courses._appetizers);

When I log this in compiler it returns:

    [ { name: undefined, price: undefined } ]

It seems like the function isn't getting called. Am I approaching this fundamentally incorrectly?

6
  • 4
    this._courses[courseName] is undefined. What is the value of courseName? Commented Mar 15, 2018 at 20:36
  • jsfiddle.net/Lxg99me0/3 works for me, it alerts undefined because you are setting dish wrong but there is no errors in console. Post how you call that function, you are most likely passing the wrong courseName Commented Mar 15, 2018 at 20:42
  • 2
    courseName does not seem to be present in _courses. Thats why it is undefined. Commented Mar 15, 2018 at 20:43
  • 1
    how are you calling it? I think you are passing a wrong key name in courseName. Commented Mar 15, 2018 at 20:43
  • I am running this on Code Academy. I want this to return an array and display that array. Currently, it's just returning undefined. I was indeed calling the key incorrectly. I am now calling the function as: menu.addDishToCourse("_appetizers", "random", 10); console.log(menu._courses._appetizers); When I log this in compiler it returns: [ { name: undefined, price: undefined } ] It seems like the function isn't getting called. Am I approaching this fundamentally incorrectly? Commented Mar 15, 2018 at 21:26

2 Answers 2

1

Your first problem is that you were passing a wrong courseName to the function, that's why you get the error.

And your second problem is that you are assigning wrong values, to object to push in your courses array when you used { name: this.dishName, price: this.dishPrice }.

In fact when you use this.dishName you were trying to get dishName from your menu object that's why it's undefined, you just need to use the parameters dishName and dishPrice you are passing arguments to the function.

    const dish = {
      name: dishName,
      price: dishPrice
    };

    this._courses[courseName].push(dish);

This is a working Demo:

let menu = {
  _courses: {
    _appetizers: [],
    _mains: [],
    _desserts: []
  },

  addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice
    };

    this._courses[courseName].push(dish);
  },
};


menu.addDishToCourse("_appetizers", "random", 10);
console.log(menu._courses._appetizers);

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

3 Comments

I thought I'd just said that earlier :)
@StevePadmore In fact it was not a scope problem, it was wrong values problem, instead of passing the arguments of the function. :)
I agree, that the wrong values were passed, but that was because 'this' was used, and the variables used are not in the scope of 'this', but in the function itself. I think it's just semantics, but the answer is still the same :)
1

It is the scope that is the issue.

this is the object itself (the menu in this case) and has _courses and addDishToCourse as its properties.

Inside the addDishToCourse this is looking at the object for the dishName and dishPrice, but they are scoped to the function itself (from the parameters).

So the correct code would be like so:

let menu = {
        _courses: {
            _appetizers: [],
            _mains: [],
            _desserts:[]
        },

        addDishToCourse(courseName, dishName, dishPrice) {
            const dish = { name: dishName, price: dishPrice };

            this._courses[courseName].push(dish);
        }
    }    

menu.addDish("_mains", "Pizza", "10");

4 Comments

Actually this is not a scope problem, it was wrong values problem, instead of passing the arguments of the function.
Scope as in dishName and dishPrice are not in the scope of the object (so this.dishName is undefined), but in the scope of the function (just dishName is correct).
Maybe, you are right as well, as they are part of the object scope ;) You are still right:)
And you as well ;)

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.