Meal Maker - Cannot read property 'push' of undefined

Hi guys!

I am on the Meal Maker exercise and I have the following error when running my code:

_this.courses[courseName].push(dish);

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

I can’t see where the mistake is though… Here is my code. Can someone please help?

let menu = {
  _courses: {
    appetizers: [],
    mains: [],
    dessert: [],
    
get appetizers() {
return this._appetizers;
 	},
get mains() {
return this._mains;
   	},
get desserts() {
return this._desserts;
   },

set appetizers(appetizerIn){
     this._appetizers.push(appetizerIn);
 	},
set mains(mainIn){
     this._mains.push(mainIn);
 	},
set desserts(dessertIn){
     this._desserts.push(dessertIn);
   }
  },
  
  get courses() {
    return {
      appetizers: this._courses.appetizers,
      mains: this._courses.mains,
      desserts: this._courses.desserts
    }
  },
  
  addDishToCourse(courseName,dishName,dishPrice) {
  const dish = {
    name: dishName,
    price: dishPrice
  };
    this._courses[courseName].push(dish);
    console.log(courseName + ' ' + dishName + ' ' + dishPrice);
},
  
  getRandomDishFromCourse: function(courseName) {
    const dishes = 
      this._courses[courseName];
    const randomIndex = Math.floor(Math.random() * dishes.length);
    },
  
  generateRandomMeal: function() {
    const appetizers = this.getRandomDishFromCourse('appetizers');
    const mains = this.getRandomDishFromCourse('mains');
    const desserts = this.getRandomDishFromCourse('desserts');
    const totalPrice = appetizers.price + mains.price + desserts.price;
    return `Your meal is ${appetizers.name}, ${mains.name}, ${desserts.name}. The price is $${totalPrice}.`;
  }  
};

menu.addDishToCourse('appetizers', 'Bruschetta', 5);
menu.addDishToCourse('appetizers', 'Chicken liver parfait', 7);
menu.addDishToCourse('appetizers', 'Smoked salmon', 6);
menu.addDishToCourse('mains', 'Sirloin steak and fries', 15);
menu.addDishToCourse('mains', 'Grilled cod and greens', 12);
menu.addDishToCourse('mains', 'Caesar salad', 10);
menu.addDishToCourse('desserts', 'Cheese selection', 7);
menu.addDishToCourse('desserts', 'Sticky toffee pudding', 6);
menu.addDishToCourse('desserts', 'Apple crumble', 5);```

Thanks for your help!
3 Likes

If you look somewhere for something and it isn’t there, then either it wasn’t put there (check whatever should have put it there) or that isn’t where you should be looking (look wherever you did put it instead)

1 Like

I know it is not common practice, but just remove the get and setters under _courses since they are already working in addDishes and get Courses… Did the trick for me. Couldn’t find the solution with the setters and setters in place.

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
    
 },
  
  get courses() {
    return {
    	appetizers: this._courses.appetizers,
      mains: this._courses.mains,
      desserts: this._courses.desserts,
    };
},
  
  ///===============================================================
  
  addDishToCourse (courseName, dishName, dishPrice) {
    let dish = {
      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} and ${dessert.name}. The price is $${totalPrice}.`;
  },
};


menu.addDishToCourse('appetizers', 'Tomato Soup', 2.50);
menu.addDishToCourse('appetizers', 'Gaspachio Soup', 3.85);
menu.addDishToCourse('appetizers', 'Salmon Roll', 5.60);
menu.addDishToCourse('mains', 'Ribs', 8.34);
menu.addDishToCourse('mains', 'Brussel Sprouts', 9.23);
menu.addDishToCourse('mains', 'Trout Filet', 10.55);
menu.addDishToCourse('desserts', 'Cheese Cake', 4.35);
menu.addDishToCourse('desserts', 'Strawberry Coupe', 6.60);
menu.addDishToCourse('desserts', 'Dame Blanche', 5.60);

let meal = menu.generateRandomMeal();
console.log(meal);
2 Likes

hey there,

just wondering that did you find the issue? i got the same error with you…

thanks!

2 Likes

const menu = {

_courses: {

  appetizers: [],

  mains: [],

  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}, ${dessert.name}, and the total price is ${totalPrice};

} 

};

menu.addDishToCourse(‘appetizers’, ‘salad’, 4.00);

menu.addDishToCourse(‘appetizers’, ‘wings’, 5.00);

menu.addDishToCourse(‘appetizers’, ‘spinich dip’, 4.50);

menu.addDishToCourse(‘mains’, ‘steak’, 12.00);

menu.addDishToCourse(‘mains’, ‘salmon’, 14.00);

menu.addDishToCourse(‘mains’, ‘chicken’, 10.00);

menu.addDishToCourse(‘desserts’, ‘apple pie’, 4.00);

menu.addDishToCourse(‘desserts,’, ‘cheesecake’, 5.00);

menu.addDishToCourse(‘desserts’, ‘carrott cake’, 4.50);

let meal = menu.generateRandomMeal();

console.log(meal);
Even though I removed the _courses getters and setters I still keep getting the same error:
/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:20
return this._courses[courseName].push(dish);
^

TypeError: Cannot read property ‘push’ of undefined
at Object.addDishToCourse
Maybe there is something that I’m not seeing here. If someone can help point that out it would be appreciated. Thanks.