Meal Maker - Cannot read property 'push' of undefined


#1

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!

#2

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)


#3

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);