Meal-maker, Cannot read property ‘price’ of undefined

https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-ii/modules/fecp-learn-javascript-syntax-objects/projects/meal-maker

Hi everybody, I have this problem and I don’t find the mistake, please help me…
Thank you very much…

const menu = {

  _courses : {

    appetizers: [],

    mains: [],

    desserts: []

  

  },

    get courses() {

      return{

        appetizers: this.appetizers,

        mains: this.mains,

        desserts: this.desserts,

      }

    },

    

  get appetizers() {

    return this._courses.appetizers;

  },

  set appetizers(appetizers) {

    this._courses.appetizers = appetizers;

  },

  get mains() {

    return this._courses.mains;

  },

  set mains(mains) {

    this._courses.mains = mains;

  },

  get desserts() {

    return this._courses.desserts;

  },

  set appetizers(desserts) {

    this._courses.desserts = 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 meal is ${appetizer.name}, ${main.name}, ${dessert.name} The total price is ${totalPrice}`;

     }

  };

  menu.addDishToCourse('appetizers','Caesar Salad', 4.25);

let meal = menu.generateRandomMeal();

console.log(meal);

This is the message error:
/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:77
const totalPrice = appetizer.price + main.price + dessert.price;
^

TypeError: Cannot read property ‘price’ of undefined
at Object.generateRandomMeal (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:77:36)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:87:17)

Have you tested your getRandomDishFromCourse() function?

The error message is suggesting that it’s returning an undefined value. So a good start is to work backwards from that issue.

Additionally, it’s useful to test functions before adding code on top. Bugs can still happen, but it takes care of a lot of needless ones.

3 Likes

Yes, you are right… I had a mistake in that funcition, and I checked, I think is right like this…

getRandomDishFromCourse(courseName){

       const dishes = this._courses[courseName];

       const randomIndex = Math.floor(Math.random() * dishes.length);

       return dishes[randomIndex];


     },

But I have the same error...

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:75
       const totalPrice = appetizer.price + main.price + dessert.price;
                                                ^

TypeError: Cannot read property 'price' of undefined
    at Object.generateRandomMeal (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:75:49)
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:85:17)
    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)

It’s the same error but it’s pointing to a different part of your code now. The forum removed some of the formatting in your original post, but it was pointing to appetizer.price when it mentioned “Cannot read property ‘price’ of undefined”, even though you added an appetizer.

Now that you’ve implemented the fixes that @toastedpitabread directed you towards, it is pointing at main.price when it gives the error. You never added any mains or desserts using menu.addDishToCourse() so you don’t have any entries for it to return.

2 Likes

then the best thing to do, would be to add a .log() to inspect the state of your program:

getRandomDishFromCourse(courseName){

       const dishes = this._courses[courseName];

       const randomIndex = Math.floor(Math.random() * dishes.length);

       console.log(dishes, randomIndex);
       return dishes[randomIndex];


     },

which shows us, we don’t have any main courses or dessert. That is a problem

2 Likes

Thank you very much…
Finally I realized that I didn´t understand the functionallity of this program, but I apreciate your help…

1 Like