Error in project Meal Maker


#1
const menu = {
  _courses: {
		appetizers: [],
		mains: [],
    deserts: [],
    },
  get courses() {
		return {
    appetizers: 
   this._courses.appetizers,
    mains: 
   this._courses.mains,
    deserts: 
   this._courses.deserts,
			}
  	},
	  set appetizers(appetizer) {
        this._appetizers.push(appetizer);
    },
    
    set mains(main) {
        this._mains.push(main);
    },
    
    set desserts(dessert) {
        this._desserts.push(dessert);
    },
    
    get appetizers() {
      return this._appetizers;
    },
    
    get mains() {
			return this._mains;
    },
    
    get desserts() {
    	return this._desserts;
  	}, 
	addDishToCourse(courseName,dishName,dishPrice){
  const 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);
  },
  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','Caesar Salad',3.25);
menu.addDishToCourse('mains', 'Soup',6.25);
menu.addDishToCourse('desserts', 'cake',2);
let meal = menu.generateRandomMeal();
console.log(meal);

I do not understand why this error occurs = /home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:46
this._courses [courseName] .push (dish);
^

TypeError: Can not read property ‘push’ of undefined
at Object.addDishToCourse

Help please.


#2

this._courses [courseName] must be undefined somehow, maybe track the value using console.log()?

because its undefined, undefined.push doesn’t work given there is no push method for undefined data type


#3

I found a mistake, in one place I wrote a dessert and in another place a desert, lose ‘s’. Thanks for the hint!


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.