Meal Maker project: TypeError: Cannot read property 'push' of undefined

Meal Maker Project

Hi! Can someone help me spot my mistake I keep getting the Cannot read property ‘push’ of undefined from the addDishToCourse section. I am unable to figure out even with the walkthrougth video my mistake that make courseName not an array.

const menu = {

  _courses: {

    appetizers: [],

    mains: [],

    desserts: [],

  }, 

  get appetizers() {

    return this._courses.appetizers;

  }, 

  get mains() {

    return this._courses.mains;

  }, 

  get desserts() {

    return this._courses.desserts ;

  },

  set appetizers(appetizers) {

    this._courses.appetizers = appetizers;

  },

  set mains(mains) {

    this._courses.mains = mains ;

   },

  set desserts(desserts) {

    this._courses.desserts = desserts ;

    },

  get courses() {

    return {

      appetizers: this.appetizers,

      mains: this.mains,

      desserts: this.desserts,

    };

  },

addDishToCourse(courseName, dishName, dishPrice) {

  const dish = {

    name: dishName,

    price: dishPrice,

    };

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

},

getRandomDishFromCourse(courseName) {

    const dishes = this._courses[courseName];

    const randomIndex = Math.floor(Math.random() * dishes.length);

    return dishes[randomIndex];

  },

generateRandomMeal() {

    const appetizer = this.getRandomDishFromCourse('appetizers');

    const main = this.getRandomDishFromCourse('mains');

    const dessert = this.getRandomDishFromCourse('desserts');

    const totalPrice = appetizer.price + main.price + dessert.price;

    return `Your meal is ${appetizer.name}, ${main.name} and ${dessert.name} for a total of ${totalPrice}. `;

  }

};

menu.addDishToCourse('appetizers', 'Cesar Salad', 4.25);

menu.addDishToCourse('appetizers', 'soup', 3.75);

menu.addDishToCourse('appetizers', 'Shrimp', 5.50);

menu.addDishToCourse('mains', 'Spaghetti', 17.25);

menu.addDishToCourse('mains', 'Steak & fries', 25.75);

menu.addDishToCourse('mains', 'Tuna steak', 32.50);

menu.addDishToCourse('desserts', 'Cheeze cake', 7.50);

menu.addDishToCourse('desserts', 'cream bruler', 6.50);

menu.addDishToCourse('dessert', 'fruit salade', 3.50);

const meal = menu.generateRandomMeal();

console.log(meal);

The walk-through video doesn’t help, if you don’t understand what the problem is. So lets tear the code apart, and lets see why we get an error:

console.log(this._courses, dishName, courseName);
return this._courses[courseName].push(dish);

Seems to go well a couple of times, until you try to add fruit salad, you made a typo in the course name.

Nice! thank you so much, one more question… we do I get the array in a general form at the .push if the real error is only a typo later on in the code? I would be expecting the typo to be undefined instead of the array.

I don’t understand the question.

trying to access a property which doesn’t exists on a object results in undefined. (which you could argue should throw an error already). Because the error you get now (push not existing on undefined) is misleading.