Meal Maker code

Hi,
I am working on the Meal Maker project and I do not understand why a I getting the error :TypeError: Cannot read property ‘push’ of undefined
at Object.addDishToCourse
My code is below:

Thank you in advance for your help!
You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

Hello, @blog1959046775, and welcome to the forums.

Learning to understand error messages will go a long way toward being able to debug your own code. Looking at the message you’ve received, let’s break down what it means.

“Cannot read property ‘push’ of undefined” … does the message also include something that would help us locate the code where the error is encountered? Yes. “at Object.addDishToCourse” So, is there a place in your code where you have an object that has a property named addDishToCourse? Yes. You have a function with that name. Do you use the Array.push() method inside that function? Yes. The error says that you are trying to call the .push() method on something that has not been defined. What would that be? What in your code is the .push() method attached to? Is it defined? If it was supposed to have been defined, then where?

Here’s an example of a very similar problem where something is undefined:

let dreams = ["own a yacht"] //dreams is defined here

dreams.push("visit the queen") //shouldn't be a problem here

console.log(dreams) //[ 'own a yacht', 'visit the queen' ]

dream.push("win the lottery") // ReferenceError: dream is not defined

Hopefully this will help you find what needs fixing.

1 Like

I know I didn’t start this thread but I also have a problem with this project and am going nuts over it - especially since when I got errors in my own code I then decided to go watch the help video and follow the developer’s code which seemed certain to succeed. But even then I still have errors.

Here is the code I used, please help. I want to become a good programmer, but there are some things I just don’t get it seems.

Eta - I ended up finding some code online that did work but looked different to mine. Bur I still would be very interested in understanding why the code I used does not work. Thanks.

Here is the code:

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 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 ${appetizer.name}, ${main.name},and ${dessert.name} and the total price is ${totalPrice}`;
  }
  };

menu.addDishToCourse('appetizers', 'salad', 3.00);
menu.addDishToCourse('appetizers', 'wings', 4.00);
menu.addDishToCourse('appetizers', 'samosas', 3.50);

menu.addDishToCourse('mains', 'steak', 12.00);
menu.addDishToCourse('mains', 'curry', 10.00);
menu.addDishToCourse('mains', 'pizza', 8.50);

menu.addDishToCourse('desserts', 'cake', 4.00);
menu.addDishToCourse('desserts', 'icecream', 6.00);
menu.addDishToCourse('desserts', 'jelly', 3.00);

const meal = menu.generateRandomMeal();
console.log(meal);

Hello @vinci_da_leo, and welcome to the forums!

What is the purpose of this method?

  get _courses(){
   return {
    appetizers: this.appetizers,
    mains: this.mains,
    desserts: this.desserts,
   };
  },

It is never called in your code, yet it presents a big problem. We cannot reference a backing variable directly in a getter like so: get _courses(). As I understand it, (there are others here who can explain this better), doing so creates a circular reference. You aren’t using/don’t need this method anyway, so I’d delete it.

Also, in addDishToCourse(), what is the purpose of return in the line:

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

Omg - I just removed that method and it worked! Thanks. Not being able to get to the bottom of it was really bugging me.

However, I got that code from a codeacademy help video that was attached to the project and that is why I included it. So, I guess maybe the videos don’t always work as a helpful reference.

Thank you so much for explaining.

1 Like