Meal Maker porperty 'push' undefined


#1

https://www.codecademy.com/courses/learn-javascript-objects/projects/meal-maker?course_redirect=introduction-to-javascript

I am always getting the following error message:

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:39
** 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:39:29)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:56:6)
at Module._compile (module.js:571:32)
at Object.Module._extensions…js (module.js:580:10)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.runMain (module.js:605:10)
at run (bootstrap_node.js:427:7)
at startup (bootstrap_node.js:151:9)

I read a previous topic about the same problem and I went through my code and corrected everything based on that, but I still get the error message above.

Can you help me what the probelem could be, please?

Thank you in advance!

const menu = {
  _courses: {
   _appetizers: [],
   _mains: [],
   _desserts: [],
  },
 
  set appetizers(chooseAppetizer) {
    this._appetizers.push(chooseAppetizer);
  },
  set mains(chooseMain) {
    this._mains.push(chooseMain);
  },
  set desserts(chooseDessert) {
    this._desserts.push(chooseDessert);
  },
  
  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}
  },
  
  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);
   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.main}. The price is $${totalPrice} total.`
 }

};

menu.addDishToCourse('appetizers', 'A', 4.25);
menu.addDishToCourse('appetizers', 'B', 2.50);
menu.addDishToCourse('appetizers', 'C', 3.00);

menu.addDishToCourse('mains', 'E', 5.25);
menu.addDishToCourse('mains', 'F', 5.50);
menu.addDishToCourse('mains', 'G', 6.00);

menu.addDishToCourse('desserts', 'H', 1.25);
menu.addDishToCourse('desserts', 'I', 1.50);
menu.addDishToCourse('desserts', 'J', 2.00);

let meal = menu.generateRandomMeal();

console.log(meal);

#2

methods belong to/are part of specific data type, like push is specific for arrays. Somehow this._courses[courseName] is undefined, so given its not an array, it will throw an error

maybe use console.log to trace how come this._courses[courseName] is undefined?


#3

the problem is with the getters of appetizers, mains and desserts, this.mains is undefined, given menu doesn’t have mains property, mains is part of courses


#4

Thank you for your help! It’s working now. :slight_smile:


#5

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