Meal Maker Issue: cannot read property push of "undefined"

Greetings. I have the following issue with the code. I have tried changing square braces for parenthesis, tried using both parenthesis and square braces and so on. Can’t seem to find why my code is not detecting de constant “dishes”

let menu = {
  _courses: {
    appetizers:[],
    mains: [],
    desserts:[],
  },
    
    get appetizers(){
      return this._courses.appetizers;
    },

    set appetizers(appetizersIn){
      this._courses.appetizers = appetizersIn; 
    },

    get mains(){
      return this._courses.mains;
    },
    
    set mains(mainsIn){
      this._courses.mains = mainsIn;
    },

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

    set desserts(dessertsIn){
      this._courses.desserts = dessertsIn;
    },

    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){
    let dishes = this.courses[courseName];
    const randomNum = Math.floor(Math.random() * dishes.length);
    return dishes[randomNum];
  },

  generateRandomMeal(){
    const appetizer = this.getRandomDishFromCourse('appetizer');
    const main = this.getRandomDishFromCourse('main');
    const dessert = this.getRandomDishFromCourse('dessert');
    const totalPrice = appetizer.price + main.price + dessert.price;
    return `The meal of the day is: ${appetizer} as appetizer ${main} as the main dish, and ${dessert} for dessert`;
  },
};

menu.addDishToCourse('appetizer', 'fries', 4.50);
menu.addDishToCourse('appetizer', 'chicken strips', 5.00);
menu.addDishToCourse('appetizer', 'salad', 3.00);
menu.addDishToCourse('appetizer', 'shrimps', 7.50);
menu.addDishToCourse('appetizer', 'fried plantain', 5.50);
menu.addDishToCourse('appetizer', 'fried yucca', 3.75);

menu.addDishToCourse('main', 'burger', 5.00);
menu.addDishToCourse('main', 'steak', 4.75);
menu.addDishToCourse('main', 'fish', 6.50);
menu.addDishToCourse('main', 'burrito', 4.50);
menu.addDishToCourse('main', 'pasta', 5.25);
menu.addDishToCourse('main', 'soup', 3.75);

menu.addDishToCourse('dessert', 'icecream', 4.75);
menu.addDishToCourse('dessert', 'brownie', 5.00);
menu.addDishToCourse('dessert', 'tiramisu', 7.75);
menu.addDishToCourse('dessert', 'cake piece', 6.50);
menu.addDishToCourse('dessert', 'smores', 5.62);
menu.addDishToCourse('dessert', 'flavored jelly', 3.25);

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

The error says cannot read property “push” of undefined. I am not sure what I did wrong in this scenario.

1 Like

This is the bit of code you should consider:

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

If you’re pushing undefined, it has to do with something in the supply-chain for dish, right? Try checking your values there with console.logs. This might mean jumping around in your code to track the functions and all that.

Thank you @toastedpitabread for replying. However I found my mistake wasn’t even in that line of code.

I fixed the courseNames on these sections

 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 `The meal of the day is: ${appetizer.dishName} as appetizer ${main.dishName} as the main dish, and ${dessert.dishName} for dessert`;
  },

and here

menu.addDishToCourse('appetizers', 'fries', 4.50);
menu.addDishToCourse('appetizers', 'chicken strips', 5.00);
menu.addDishToCourse('appetizers', 'salad', 3.00);
menu.addDishToCourse('appetizers', 'shrimps', 7.50);
menu.addDishToCourse('appetizers', 'fried plantain', 5.50);
menu.addDishToCourse('appetizers', 'fried yucca', 3.75);

menu.addDishToCourse('mains', 'burger', 5.00);
menu.addDishToCourse('mains', 'steak', 4.75);
menu.addDishToCourse('mains', 'fish', 6.50);
menu.addDishToCourse('mains', 'burrito', 4.50);
menu.addDishToCourse('mains', 'pasta', 5.25);
menu.addDishToCourse('mains', 'soup', 3.75);

menu.addDishToCourse('desserts', 'icecream', 4.75);
menu.addDishToCourse('desserts', 'brownie', 5.00);
menu.addDishToCourse('desserts', 'tiramisu', 7.75);
menu.addDishToCourse('desserts', 'cake piece', 6.50);
menu.addDishToCourse('desserts', 'smores', 5.62);
menu.addDishToCourse('desserts', 'flavored jelly', 3.25);

The only difference in those updated sections of code is that both are plural when they were singular compared to the last code. Turning both strings to plural made the code work like a charm. However, I don’t seem to find why these strings should be plural for the code to work. I think it has something to do with the name of our arrays at the beginning or because of the getters. but then I thought it wouldn’t make sense as these are how the objects are named. The way I understand it is that

something != ‘something’

I made my code work, but can’t figure out why.

Edit: I also added the dishName element so it could be read aswell.

Yes.

In this line:

return this._courses[courseName].push(dish); //aside: you don't actually need return

you are calling the .push method on this._courses[courseName] Where is courseName defined? It is one of the parameters of your function. When you call the function, and supply 'appetizer' as the argument instead of 'appetizers' what happens? You’ve already seen what happens. this._courses['appetizer'] is undefined (it doesn’t exist), so you get the error. this._courses['appetizers'], however, is defined in the code below along with this._courses['mains'] and this._courses['desserts']:

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

_courses is an object with properties named (keys) appetizers, mains and desserts. There are no keys appetizer, main or dessert.

1 Like