I get error 'Cannot read property 'push' of undefined'

Hello everyone, I am doing a Meal Maker exercise in the Object part of JavaScript, even though I followed the instructions, I still get the following error TypeError: Cannot read property ‘push’ of undefined. Please help me to determine where I went wrong. Thank you.

Meal maker exercise
Learn JavaScript Syntax: Objects | Codecademy

And the following is my 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 getIndex = Math.floor(Math.random()*dishes.length); return dishes[getIndex]; }, 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}, ${dessert.name} The price is ${totalPrice}.`; } }; menu.addDishToCourse('appetizers', 'Caesar Salad', 4.25); menu.addDishToCourse('appetizers', 'Tráng miệng 1', 4.1); menu.addDishToCourse('appetizers', 'Tráng miệng 2', 4.9); menu.addDishToCourse('mains', 'Món chính 1', 12); menu.addDishToCourse('mains', 'Món chính 2', 5); menu.addDishToCourse('mains', 'Món chính 3', 7); menu.addDishToCourse('deserts', 'Tráng miệng 1', 7); menu.addDishToCourse('deserts', 'Tráng miệng 2', 8); menu.addDishToCourse('deserts', 'Tráng miệng 3', 9); let meal = menu.generateRandomMeal(); console.log(meal);

Hi,
this is the error thrown:

/home/ccuser/script.js:67
    return this._courses[courseName].push(dish);
                                     ^

TypeError: Cannot read property 'push' of undefined

That means that this._courses[courseName] is ‘undefined’. Console.log the parameters of the method addDishToCourse and see what arguments were passed to that method. What do you get as an argument for the parameter ‘courseName’ ? Does it match with the keys of this object:

_courses: {

    appetizers: [],

    mains: [],

    desserts: [],

  },

?

1 Like

Thanks for your reply

But, let’s take a look with the following answer for Meal Maker exercise. It’s the same as my code, but without errors

const menu = { _courses: { _appetizers: [], _mains: [], _desserts: [], get appetizers() { return this._appetizers; }, set appetizers(appetizersIn) { this._appetizers = appetizersIn; }, get mains() { return this._mains; }, set mains(mainsIn) { this._mains = mainsIn; }, get desserts() { return this._desserts; }, set desserts(dessertsIn) { this._desserts = dessertsIn; }, }, get courses() { return { appetizers: this._courses.appetizers, mains: this._courses.mains, desserts: this._courses.desserts, }; }, ///=============================================================== addDishToCourse (courseName, dishName, dishPrice) { const dish = { name: dishName, price: dishPrice, }; 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}. The price is $${totalPrice.toFixed(2)}.`; }, }; menu.addDishToCourse('appetizers', 'Ceasar Salad', 4.25); menu.addDishToCourse('appetizers', 'Prawn Coctail', 4.25); menu.addDishToCourse('appetizers', 'Garlic Bread', 3.50); menu.addDishToCourse('mains', 'Lasagna', 9.75); menu.addDishToCourse('mains', 'Ribeye Steak', 14.95); menu.addDishToCourse('mains', 'Fish & Chips', 12.95); menu.addDishToCourse('desserts', 'Cheese Cake', 4.50); menu.addDishToCourse('desserts', 'Creme Brule', 4.25); menu.addDishToCourse('desserts', 'Cheese Board', 3.25); let meal = menu.generateRandomMeal(); console.log(meal);

If the code runs without errors while yours doesn’t it is not the same…
Did you follow my advise and logged the arguments passed to the method addDishToCourse? If you did, this is what you would’ve got:

console.log(courseName)
// logs:
"appetizers"
"appetizers"
"appetizers"
"mains"
"mains"
"mains"
"deserts"

This is what your getter picks from the _courses object:

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

See the difference?

1 Like