Meal Maker Confused with errors

Hi everyone I’m new to JS and for the life of me i can’t figure out this error with the MealMaker project. any advise would be great. I added a console.log(courseName to see if anything was being passed through which it is but then I get the following errors.

return this._courses.mains;

TypeError: Cannot read property ‘mains’ of undefined
at Object.get mains [as mains] (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:10:27)
at Object.addDishToCourse (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:38:25)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:57: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)

My code is below.

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);
    const dishes = this._courses[courseName]
    const randomDish = Math.floor(Math.random() * dishes.length);
    return dishes[randomDish];
  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 `The total cost for your meanl is £ ${totalPrice}.`;

//menu.addDishToCourse('appetizers', 'Hummus', 3.50);
//menu.addDishToCourse('appetizers', 'Spring rolls', 2.00);
//menu.addDishToCourse('appetizers', 'Salad', 2.50);
menu.addDishToCourse('mains', 'Spagetti', 7.50);
menu.addDishToCourse('mains', 'Brisket', 11.50);
menu.addDishToCourse('mains', 'Lanb Shank', 13.50);
menu.addDishToCourse('desserts', 'Chessecake', 4.50);
menu.addDishToCourse('desserts', 'Belgian Waffle', 5.50);
menu.addDishToCourse('desserts', 'Lava Cake', 6.00);

let meal = menu.generateRandomMeal();

Hi guys, not sure if anyone viewed it yet but I managed to figured out the issue. I defined a getter and setter for courses inside the definition or the _courses object. once i put this outside the definition it worked fine.


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