Meal maker project : Cannot read property 'push' of undefined

Hello every one, i’m having a hard time trying to solve this error message.
The code is exactly (at least it’s suppose to be) the same as the one from the solution video however i keep having this error message

can anybody help me find what’s wrong this return line

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

const menu = {
  _courses: {
    appetizers : [],
    mains : [],
    desserts : [],
    get appetizers(){
      return this._courses.appatizers;
    }, 
    set appetizers(appetizerIn){
      this._courses.appetizers = appetizerIn;
    }, 
    get mains(){
      return this._courses.mains;
    },
    set mains(mainIn){
      this._courses.mains = mainIn;
    },
    get desserts(){
      return this._courses.desserts;
    }, 
    set desserts(dessertIn){
      this._courses.desserts = dessertIn;
    }
  }, 
  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}, and the total price is ${totalprice}`;
  }
};


menu.addDishToCourse('appetizers', 'salad', 4.00);
menu.addDishToCourse('appetizers', 'pizzeta', 5.00);
menu.addDishToCourse('appetizers', 'rizoto', 6.50);


menu.addDishToCourse('mains', 'steack', 14.00);
menu.addDishToCourse('mains', 'pizza', 15.00);
menu.addDishToCourse('mains', 'pasta', 12.50);

menu.addDishToCourse('desserts', 'cream', 2.50);
menu.addDishToCourse('desserts', 'cake', 2.50);
menu.addDishToCourse('desserts', 'cream', 2.50);

const meal = menu.generateRandomMeal();
console.log(meal);

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:37
    return this._courses[courseName].push(dish);
                                    ^

TypeError: Cannot read property 'push' of undefined
    at Object.addDishToCourse (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:37:37)
    at Object.<anonymous>
...

we could start by figuring out where the undefined comes from. This can be achieve by logging the data involved:

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

so, this._courses gives us:

[object Object] {
  appetizers: undefined,
  desserts: undefined,
  mains: undefined
}

Do you have any idea why this might be?

i found few errors since after your message.


    desserts : [],
    get appetizers(){

missing a closing bracket here.
and here

get _courses(){

get course()

and finaly here

> ${totalprice}; where i shoud write totalPrice

Thanks. i got very confused with this chapter. i guess i need to take a step back to try to understand what i’m doing first. :slight_smile: