Meal Maker - Cannot read property of Push of Undefined

For some reason, on the meal maker project, I have made my code look exactly like the video and I am still getting a cannot read property of Push of undefined for the addDishToCourse function…

I have getters and setters in for all the _courses arrays… any idea what is wrong?

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },
   get appetizers() {
    return this._appetizers;
  },
    get mains() {
    return this._mains;
  },
   get desserts() {
    return this._desserts;
  },
  get courses() {
	return {
		appetizers: this._courses.appetizers,
		mains: this._courses.mains,
		desserts: this._courses.desserts,
	}
  },
   set appetizers(appetizer) {
    this._appetizers.push(appetizer);
  },
  set mains(main) {
    this._mains.push(main);
  },
  set desserts(dessert) {
  	this._desserts.push(dessert);
  },
  get courses() {
    return {
      appetizers: this.appetizers,
      mains: this.mains,
      desserts: this.desserts,
    };
  },
	addDishToCourse (courseName, dishName, dishPrice) {
    const dish = {
      course: courseName,
      name: dishName,
      price: dishPrice,
    };
  	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}, ${dessert.name}, The price is $${totalPrice}.` ;
  }
}


menu.addDishToCourse('appetizers', 'quesadilla', 12);
menu.addDishToCourse('appetizers', 'wings', 11);
menu.addDishToCourse('appetizers', 'tacos', 9);

menu.addDishToCourse('main', 'pizza', 15);
menu.addDishToCourse('main', 'pasta', 16);
menu.addDishToCourse('main', 'steak', 25);

menu.addDishToCourse('desserts', 'ice cream', 5);
menu.AddDishToCourse('desserts','cheesecake', 7);
menu.addDishToCourse('desserts','creme brulee', 6);

let meal = menu.generateRandomMeal()

console.log(meal)

Your attribute name is mains, so this line and the two that follow will raise an exception.

classic- of course. thank you!

1 Like