Meal Maker

I’m having trouble with the meal maker project. I attempted it myself first and found that I was getting errors I didn’t understand so I followed along with the dev-walkthrough video quite closely. However, now that I have finished, I am getting the following error:

TypeError: Cannot read property ‘push’ of undefined
at Object.addDishToCourse

and the relevant line of code:

addDishToCourse(courseName, dishName, dishPrice) {
const dish = {
name: this.dishName,
price: this.dishPrice,
};
return this._courses[courseName].push(dish);
},

If I understand correctly, the TypeError is because I am calling the .push() method on an Object, not an array, thus the error. But I don’t understand why this would produce an error; am I not calling .push() on a property of the menu object which is in fact an array?

undefined.push

No. You’re attempting to call push on a property of the _courses object which is itself a property of the menu object. What you are actually doing is, as @ionatan has shown, calling push on something that is undefined (doesn’t exist).

Try adding a console.log statement or few to see what the values in question are. For instance:

  addDishToCourse(courseName, dishName, dishPrice) {
    const dish = {
      name: this.dishName,
      price: this.dishPrice,
    };
    console.log(this._courses[courseName]); //debug print
    console.log('courseName: ', courseName); //debug print
    return this._courses[courseName].push(dish); //aside: you don't need return here
  },