Meal Maker

This error keeps coming up, don’t know how to fix it. Please help!
Error:
/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:39)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:61: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)

My code:

const menu = {

  _courses: {

    appetizers: [],

    mains: [],

    desserts: [],

  },

  get appetizers() {

     return this._courses.appetizers;

  },

  set appetizers(appetizersIn) {

    return this._courses.appetizers = appetizersIn;

  },

  get mains() {

    return this._courses.mains;

  },

  set mains(mainsIn) {

    this._courses.mains = mainsIn;

  },

  get desserts() {

    this._courses.dessert;

  },

  set desserts(dessertsIn) {

    this._courses.dessert = dessertIn;

  },

  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 + mains.price + dessert.price;

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

  },

};

You don’t call this method anywhere? Seems you didn’t post your full code?

You could log information:

    console.log(this._courses, courseName);
    return this._courses[courseName].push(dish);

to help you trace your error

1 Like

That helped, thanks! It was a mistake outside the object. Although know another error comes up, any thought?

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:45
const appetizer = this.getRandomDishFromCourse(appetizers);
^

ReferenceError: appetizers is not defined
at Object.generateRandomMeal (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:45:52)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:66:19)
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)

Hey there! I think you should surround appetizers with quotes, since it should be a string and not a variable.

Hope this helps :upside_down_face:

1 Like