Meal Maker - error message I just CANNOT work out

Hey guys,

I’ve been stuck on this bit of the course for over a week now as I’m completely lost as to what is causing this error, please help!

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) {
    return this._courses.appetizers = appetizers;
  },
  set mains(mains) {
    return this._courses.mains = mains;
  },
  set desserts(desserts) {
    return 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);
  },
  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 = getRandomDishFromCourse('desserts');
    const totalPrice = appetizer.price + main.price + dessert.price
    return `Your meal consisted of ${appetizer.name}, ${main.name}, ${dessert.name}. The price is $${totalPrice}.`;
  }
};

menu.addDishToCourse('appetizers','olives', 3.00);
menu.addDishToCourse('appetizers','nachos', 5.00);
menu.addDishToCourse('appetizers','garlic bread', 3.50);

menu.addDishToCourse('mains','lasagne', 12.00);
menu.addDishToCourse('mains','pizza', 12.50);
menu.addDishToCourse('mains','ravioli', 10.50);

menu.addDishToCourse('dessert','chocolate fondant', 7.00);
menu.addDishToCourse('dessert','tiramisu', 8.00);
menu.addDishToCourse('dessert','cheesecake', 7.50);

const meal = menu.generateRandomMeal();

console.log(meal);


The error message I’m given is:

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

It’s probably something really obvious I just can’t work it out…

Thanks!

Hi @xoxoevie
welcome to the forum!

It’s always helpful to add a console to the function that throws the error.
If you add console.log(dish, courseName); you find out which arguments were passed to the function and compare them with the key names in your object to which you want to push the dish.

here:

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

when you try to use a key which does not exists on the object, we get undefined (personally, I think JavaScript should have thrown an error instead), so then the first step would be to find out what the wrong key is:

  addDishToCourse (courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice,
    };
    console.log(Object.keys(this._courses), courseName);
    return this._courses[courseName].push(dish);
  },

I added a .log() to see the available keys and the keys you attempt to use. Do you see the problem?

Thank you for replying so quickly! I don’t, my brain hurts & I’m still really confused haha, I am completely lost. I’ve even tried comparing it to the walkthrough video and am still missing it somehow…

What did the console show?
These are the keys you’re trying to push your dishes to:

appetizers: [],
    mains: [],
    desserts: [],

When you compare it to console.log(courseName); is it the same?

Btw, you don’t need to return from that function.

I personally think this is a terrible approach, then you are actual understanding the (behavior of) the code.

Did you insert the .log we suggested and looked at the output?

It’s not logging anything, just the error message I already had - but I’ve just added

console.log(dish, courseName);

to the bottom of my code, is there somewhere else I should be logging it? Excuse my ridiculously amateur questions!

If you still use the return keyword, the console.log isn’t executed, because anything after the return statement isn’t executed anymore.
This will log something:

addDishToCourse (courseName, dishName, dishPrice) {
    const dish = {
      name: dishName,
      price: dishPrice,
    };
    console.log(dish, courseName);
   this._courses[courseName].push(dish);
  }

Ahhh, yeah that makes sense.

The new error message I’ve got is this:

objects-meal-maker/app.js:70
addDishToCourse (courseName, dishName, dishPrice) {
^
SyntaxError: Unexpected token {
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
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)

Oops, a syntax error wasn’t there before. Did you forget the comma after the method when you replaced it?

Have just re entered the code you suggested and now above the original error message I had, I now have this:

[ ‘appetizers’, ‘mains’, ‘desserts’ ] ‘appetizers’
[ ‘appetizers’, ‘mains’, ‘desserts’ ] ‘appetizers’
[ ‘appetizers’, ‘mains’, ‘desserts’ ] ‘appetizers’
[ ‘appetizers’, ‘mains’, ‘desserts’ ] ‘mains’
[ ‘appetizers’, ‘mains’, ‘desserts’ ] ‘mains’
[ ‘appetizers’, ‘mains’, ‘desserts’ ] ‘mains’
[ ‘appetizers’, ‘mains’, ‘desserts’ ] ‘dessert’

That’s the key to the error now. Don’t you notice a difference between the object keys and the arguments you passed?

I am completely lost to be honest which is why I’ve not really done anything on this course for a while, it’s been about a month since I wrote this code and I’ve just been stuck on this it’s so frustrating!

Could you explain to me what I’ve done? I’m struggling to understand JavaScript at all.

You want to push the new dishes to the arrays in the _courses object:

_courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },

You are logging the key names with this

With this function call, you want to add desserts:

menu.addDishToCourse('dessert','cheesecake', 7.50);

And with this console.log(courseName); you log what you typed as a courseName.
Here you are comparing the keys to the argument:

And they don’t match. There is no key called dessert.

Unfortunately, that’s not the only problem. Once you corrected that, you’ll encounter the next error. But that should also be found with the same approach: Logging each variable in each method.

Ah, thank you. I’ll work on that and see how I get on!