Project Meal Maker Issue

When trying to do the meal-maker project I cannot get this error to go away:

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

This is my code: (I double checked with the video)

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){
this._courses.appetizers = appetizers;
},
set appetizers(mains){
this._courses.mains = mains;
},
set appetizers(desserts){
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,
};

// THIS IS WHERE THIS ISSUE IS OCCURRING

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(‘main’);
const dessert = this.getRandomDishFromCourse(‘dessert’);
const totalPrice = appetizer.price + main.price + dessert.price;
return Your meal is ${appetizer.name}, ${main.name}. and ${dessert.name}, and the total price is ${totalPrice}.
},
};

menu.addDishToCourse(‘appetizers’, ‘salad’, 5.00)
menu.addDishToCourse(‘appetizers’, ‘fries’, 5.00)
menu.addDishToCourse(‘appetizers’, ‘fruit’, 5.00)
menu.addDishToCourse(‘main’, ‘steak’, 24.00)
menu.addDishToCourse(‘main’, ‘fish’, 16.00)
menu.addDishToCourse(‘main’, ‘burger’, 25.00)
menu.addDishToCourse(‘dessert’, ‘ice cream’, 5.00)
menu.addDishToCourse(‘dessert’, ‘coffee’, 5.00)
menu.addDishToCourse(‘dessert’, ‘shake’, 5.00)

const meal = menu.generateRandomMeal();

console.log(meal)

It seems like it’s not taking in my parameters for my object addDishToCourse (the parameters are supposed to be blue and they are showing up as red). I don’t know why.

Very frustrating!

Did you try to print the courses?

console.log(menu.courses)

What did you get?

Hello, @cellerius, and welcome to the forums.

Interesting way to put it. What we have to do when our code doesn’t do what we wanted it to, is make observations about what it is doing instead. You are wanting to use the .push method to add dish to an array.

This message is telling you that you are calling the .push method on something that is undefined (doesn’t exist) instead of on an array. How can you tell what you are calling the .push method on? One relatively easy way is to use console.log to see what values are in play. For example:

addDishToCourse(courseName,dishName,dishPrice) {
  const dish = {
    name: dishName,
    price: dishPrice,
  };

 // THIS IS WHERE THIS ISSUE IS OCCURRING
 // Let's print, and see what is going on
 console.log(`courseName: ${courseName}  dish: ${dish}\n${this._courses[courseName]}`);
 return this._courses[courseName].push(dish); //FYI No need for return here.

},

Consider this much more simple example that throws the same error you have:

const dogs = {
  breeds: []
};

dogs.breeds.push('collie');

console.log(dogs.breeds); //[ 'collie' ]
// so far, so good

dogs.breed.push('german shepherd');
// TypeError: Cannot read property 'push' of undefined

Also, for future reference, please review How do I format code in my posts?

1 Like

Hi, I have the same problem, although followed the project walk through still have the same issue

here is the error
/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:38
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:38:35)
at Object. (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:57: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)

and here is the code:
onst menu ={

_courses: {

appetizers:[],

mains:[],

desserts:[]

},

get appetizers(){

return this._courses.appetizers;

},

get mains(){

return this._courses.mains;

},

get desserts(){

return this._courses.mains;

},

set appetizers(appetizers){

this._courses.appetizers = appetizers;

},

set mains(mains){

this._courses.mains = mains;

},

set desserts(desserts){

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= this.getRandomDishFromCourse('desserts');

const totalPrice = appetizer.price + main.price + dessert.price;

return `your meal is ${appitizer.name} and ${main.name} and ${dessert.name} and the total price is ${totalPrice}`;

}

};

menu.addDishToCourse(‘appetizer’, ‘salad’, 5000);

menu.addDishToCourse(‘appetizer’, ‘salad’, 5000);

menu.addDishToCourse(‘appetizer’, ‘salad’, 5000);

menu.addDishToCourse(‘main’, ‘salad’, 5000);

menu.addDishToCourse(‘main’, ‘salad’, 5000);

menu.addDishToCourse(‘main’, ‘salad’, 5000);

menu.addDishToCourse(‘dessert’, ‘salad’, 5000);

menu.addDishToCourse(‘dessert’, ‘salad’, 5000);

menu.addDishToCourse(‘dessert’, ‘salad’, 5000);

const meal= menu.generateRandomMeal();

console.log(meal);

Hello, @az16, and welcome to the forums.

Consider what you are trying to do here. Can you explain it in your own words to me?

Hi, this is just for the purpose of testing this method,
addDishToCourse(courseName,dishName,dishPrice){

const dish ={

  name:dishName,

  price:dishPrice

};

Thanks

What is each part of this:

What does the addDishToCourse method do with the arguments you supply?

I’m not asking out of curiosity. I’m asking to get you to think through what is happening with your code. Run the code in your head. When ‘appetizer’ is supplied as the first argument, what happens with it?

appetizer gets assigned to the parameter courseName. Then what?

1 Like