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

I keep getting this error on this line:
return this._courses[courseName].push(dish);

TypeError: Cannot read property ‘push’ of undefined

but unsure what is causing it. Here is my code:

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 main(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 randomNumber = Math.floor(Math.random() * dishes.length);
    return dishes[randomNumber];
  generateRandomMeal() {
    const appetizer = getRandomDishFromCourse('appetizers');
    const main = getRandomDishFromCourse('mains');
    const dessert = getRandomDishFromCourse('desserts');
  const totalPrice = appetizer.price + main.price + dessert.price;
  return `Your meal is ${appetizer.name}, ${main.name}, ${dessert.name}, and the total price is ${totalPrice}`;




const meal = menu.generateRandomMeal();

Hello, and welcome to the forums!

You added a console.log in your addDishToCourse() method, so I know you’re on the right track to debug. Did you notice anything helpful in the output? If you add another log of just the courseName , it may be easier to spot.

Click here for another hint

You may have noticed that one course name is missing from the console.log() calls

Double check the names of the courses when you’re adding them all towards the bottom with your menu.addDishToCourse() calls. The error began on a specific one. One of the names of the courses doesn’t match the array name in _courses.

After you fix that, you’ll see a different error that you’ll be able to work on

1 Like