Meal Maker Help!

Hello! Im having issues with the Meal Maker javascript project. Keep receiving a Cannot read property “push” of undefined. I’ve read through this topic as well as watch the help video numerous times to no avail!!! Please assist! Its driving me mad! :smile:

const menu = {
  _courses : {
    appetizers : [],
    mains : [],
  desserts: [],
      },
  get appetizers() {
    return this._courses.appetizers;
  },
get mains() {
  return this._courses.mains;
},
get desserts() {
    return this._courses.desserts;
  },
set appetizers(appetizers) {
this._courses.appetizers = appetizers; 
},
  set mains(mains) {
   this._courses.mains = mains; 
  },
  set desserts(desserts) {
    this._courses.desserts = 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 meai is ${appetizer.name}, ${main.name}, ${dessert.name}. The total price for your meal is ${totalPrice}`;
    }
};
menu.addDishToCourse('Course Name', 'Feta Plate', 5.22);
menu.addDishToCourse('Course Name', 'Feta Plate', 5.22);
let meal = menu.generateRandomMeal();
console.log(meal);

error message below

/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:33)
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:53: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)[https://www.codecademy.com/courses/introduction-to-javascript/projects/meal-maker?action=resume_content_item](https://www.codecademy.com/courses/introduction-to-javascript/projects/meal-maker?action=resume_content_item)

Blockquote

1 Like

You indeed can’t.

> undefined.push

Which should tell you where you got undefined, generally either because you looked in the wrong place or didn’t put anything where there should have been. That in turn tells you to consider which place should be looked in, and if it’s the right one then it’s the code that should have put something there that’s wrong, or otherwise it’s the code that’s looking there.

Real life example:

Error, can’t sit on air -> fell onto floor.
Was there supposed to be a chair there?
Who was responsible for making sure a chair was there?

1 Like