Meal Maker project

Please can someone help. Ive been trying to find out why I get this error.

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:51
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:51:33)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:70: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)

I have logged the courses which return no entries in the array. I have got to the point of staring at it hoping to find my mistake.

I am finding this the biggest step up in difficulty so far!

Hi @method6969139750
the error is:

TypeError: Cannot read property ‘price’ of undefined

So let’s see where you try to get the property ‘price’:

That line can be found in the method ‘generateRandomMeal’. So let’s see what you get there:

generateRandomMeal() {
    const appetizer = this.getRandomDishFromCourse("appetizers");
    const main = this.getRandomDishFromCourse("mains");
    const dessert = this.getRandomDishFromCourse("desserts");
    console.log(appetizer, main, dessert);
//undefined, undefined, undefined

So getRandomDishFromCourse is not returning dishes. Let’s check:

getRandomDishFromCourse(courseName) {
  
    const dishes = this._courses[courseName];
    const randomIndex = Math.floor(Math.random() * dishes.length);
    console.log(courseName, dishes, randomIndex); //"appetizers", [], 0 "mains", [], 0 "desserts", [], 0

The course arrays are empty.
So let’s go to the function where the dishes are pushed to the arrays:

addDishToCourse(courseName, dishName, dishPrice) {
 		console.log('courseName' ,courseName); 

This console doesn’t log anything. That means this method is not called correctly. So go back to where you call the methods.

menu.addDishToCourse['appetizers', 'Garden worms', 4.00];
menu.addDishToCourse["appetizers", "Pigeon eggs", 4.50]
menu.addDishToCourse["appetizers", "Snail slime", 2.00]

menu.addDishToCourse["mains", "Seagull stew", 8.00]
menu.addDishToCourse["mains", "Cow hoof pealla", 9.50]
menu.addDishToCourse["mains", "Whale eye soup", 10.00]

menu.addDishToCourse["desserts", "Bird poo mouse", 5.00]
menu.addDishToCourse["desserts", "Hedgehog ice cream", 4.50]
menu.addDishToCourse["desserts", "Wild deer milk", 3.00]

There is something very wrong with this.

If you’re stuck, add consoles anywhere you can, and find out which console logs a valid value and which doesn’t. This way you can trace the variables and spot the error.

I cant believe I missed the fact I used square brackets for some strange reason!

Thanks for looking and pointing to the obvious mistake. If you ever fancy Snail slime, Whale eye soup and Bird poo mouse, let me know. :laughing:

Thanks

1 Like

You’re welcome.
I’m good. Enjoy your meal!