Help needed! meal maker project "Cannot read property 'push' of undefined at Object.addDishToCourse"

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 = main.price + dessert.price + appetizer.price;
return Your meal is ${main.name}, ${appetizer.name}, ${dessert.name} and your total is ${totalPrice}
}
};

menu.addDishToCourse(‘what’, ‘what’, 4.50);
menu.addDishToCourse(‘what’, ‘what’, 4.50);
menu.addDishToCourse(‘what’, ‘what’, 4.50);

If the thing you’re trying to get push from is undefined, then, you either looked for it in the wrong place or didn’t define it in the first place.

1 Like

i don’t get it :(, what should i do? what would you do in this case?

Arrays have a push attribute.
undefined does not have a push attribute.

Do: get push from arrays
Don’t: get push from undefined

problem: I have undefined somewhere… define it. or, look in the place where it’s defined if was looking in the wrong place

> a = []
[]
> b.push // wrong place, there's no b, use a, or define b
Thrown:
ReferenceError: b is not defined
1 Like

i’m sorry if i’m annoying, i’m a n00b, i just don’t understeand why it works with the walkthrough guide and not with me, so basically courseName, which is supposed to be an array, wasn’t defined in the first place? i need to create it, or change the source where i wanna push dish into right?

Something to realize is that humans are bad at copying things accurately
And also, you’re better off not copying and instead getting an idea of what should happen and then writing it from that understanding.
So if you think of it as a chain of custody for that value, then you’d start checking whatever that chain was supposed to be.
Codecademy’s instruction may be in terms of “write this then that then this” and not so much about what is at all going on. But I still think that is what you need to be basing your reasoning on.

1 Like

Also, mind you, it’s not fine to allow the forum to apply formatting to your code. Everything has meaning, so it needs to be an exact copy, nothing less.

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 = main.price + dessert.price + appetizer.price;
   return `Your meal is ${main.name}, ${appetizer.name}, ${dessert.name} and your total is ${totalPrice}`
 }
};

menu.addDishToCourse('what', 'what', 4.50);
menu.addDishToCourse('what', 'what', 4.50);
menu.addDishToCourse('what', 'what', 4.50);
/home/nate/tmp/derp/js/h.js:37
    return this._courses[courseName].push(dish);
                                     ^

TypeError: Cannot read property 'push' of undefined
    at Object.addDishToCourse (/home/nate/tmp/derp/js/h.js:37:38)

this._courses[courseName] is what you should be looking closer at. Should it be there? Is it? Is that the right name? Are you using it in a suitable way? What key is used?

1 Like

@ionatan has you on the right track. Look at the line he referenced: this._courses[courseName], and fill in the value(s) assigned. What value is assigned to courseName at this point?

1 Like

@midlindner I am having the same issue as @rgarciadc . courseName is a parameter. What’s intriguing is I also referenced Codecademy’s direct tutorial (only difference are the courses added) and still received the same error of ‘Cannot read property ‘push’ of undefined at Object.addDishToCourse’. This isn’t the first time I followed the tutorial exactly and received an error. I’d appreciate your (or others’) input.

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('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}. The price is $${totalPrice}.`;
  }
};

menu.addDishToCourse('appetizers', 'Quesadillas', 4.45);
menu.addDishToCourse('appetizers', 'Triple Dippers', 6.00);
menu.addDishToCourse('appetizers', 'Chips & Salsa', 3.00);
menu.addDishToCourse('main', 'Burger & Fries', 12.00);
menu.addDishToCourse('main', 'TexMex Bowl', 11.00);
menu.addDishToCourse('main', 'Wow Burger', 9.00);
menu.addDishToCourse('dessert', 'Cake', 4.45)
menu.addDishToCourse('dessert', 'Ice Cream', 4.45)
menu.addDishToCourse('dessert', 'Apple Pie', 4.45)

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

courseName is very important in addDishToCourse(). Make sure it’s correct or you won’t be able to access the correct key/value pairs inside of your _courses object.

1 Like

Thank you @ktsotras. I agree. Can you please elaborate with tips on verifying whether it is correct? Again I’m comparing directly from Codecademy’s tutorial (posted below) and there is no difference (at least not one that I see). I appreciate your input.

You’re looking in the wrong place. Sometimes the code isn’t the problem. If you pass data into a function in a way that the function is not expecting, that can cause errors even though the function itself is correct.

1 Like

I found out my errors. I was passing 'main' instead of 'mains' and 'dessert' instead of 'desserts' in both when invoking menu.addDishToCourse and defining my generateRandomMeal() method. Thank you for the dialogue. The correct code for this section should look like this:

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}.`;
  }
};

menu.addDishToCourse('appetizers', 'Quesadillas', 4.45);
menu.addDishToCourse('appetizers', 'Triple Dippers', 6.00);
menu.addDishToCourse('appetizers', 'Chips & Salsa', 3.00);
menu.addDishToCourse('mains', 'Burger & Fries', 12.00);
menu.addDishToCourse('mains', 'TexMex Bowl', 11.00);
menu.addDishToCourse('mains', 'Wow Burger', 9.00);
menu.addDishToCourse('desserts', 'Cake', 4.45)
menu.addDishToCourse('desserts', 'Ice Cream', 4.45)
menu.addDishToCourse('desserts', 'Apple Pie', 4.45)

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