Meal Maker - "Cannot read property" Error

Hey everyone, I was working on this project today and even though I think I made everything correctly, I get the error below:

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:7
      return this._courses.appetizers;
                          ^

TypeError: Cannot read property 'appetizers' of undefined
    at Object.get appetizers [as appetizers] (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:7:27)
    at Object.addDishToCourse (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:38:18)
    at Object.<anonymous> (/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)

And for those who wonder my code, here it is:

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
    get appetizers() {
      return this._courses.appetizers;
    },
    set appetizers(appetizer) {
      this._courses.appetizers = appetizer;
    },
    get mains() {
      return this._courses.mains;
    },
    set mains(main) {
      this._courses.mains = main;
    },
    get desserts() {
      return this._courses.desserts;
    },
    set desserts(dessert) {
      return this._courses.desserts = dessert;
    },
  },
  get courses() {
    return {
      appetizers: this.appetizers,
      mains: this.mains,
      desserts: 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);
    
    return dishes[randomIndex];
  },
  generateRandomMeal() {
    const appetizer = getRandomDishFromCourse(appetizers);
    const main = getRandomDishFromCourse(mains);
    const dessert = getRandomDishFromCourse(desserts);
    const totalPrice = appetizer.price + main.price + dessert.price;

    return `Your meal is ${appetizer.name}, ${main.name}, ${dessert.name}. Your price is ${totalPrice}.`
  }
};

menu.addDishToCourse('appetizers', 'salad', 4.00);
menu.addDishToCourse('appetizers', 'wings', 4.50);
menu.addDishToCourse('appetizers', 'fries', 5.00);

menu.addDishToCourse('mains', 'steak', 10.25);
menu.addDishToCourse('mains', 'salmon', 7.75);
menu.addDishToCourse('mains', 'tofu', 11.20);

menu.addDishToCourse('desserts', 'ice cream', 3.00);
menu.addDishToCourse('desserts', 'coffee', 3.00);
menu.addDishToCourse('desserts', 'cake', 3.25);

const meal = menu.generateRandomMeal();

console.log(meal);

Hi @autobahnmaus
your getters and setters are in the wrong place: They should be inside the menu object, not inside the _courses object.
And the setter is not supposed to return anything:

Thank you so much for your help. I’ve been trying to find my problem for hours. :smiley:

1 Like