Meal Maker .push error JavaScript Project


I still do not understand why i receive the the following error:
TypeError: Cannot read property 'push' of undefined at Object.addDishToCourse
, my code appears to mimic that on the video help file.
I checked other posts on this topic and the errors all seem to be related to spelling issues, I have gone through my code a number of times and I can not see any mismatches in the spellings.

appreciate any guidance

const menu = {
    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 this._courses
addDishToCourse(courseName, dishName, dishPrice) {
  const dish = {
    name: dishName,
    price: dishPrice,
  return this._courses[courseName].push(dish);
getRandomDishFromCourse: function (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 ${}, ${}, and ${} and the total price is 

menu.addDishToCourse('appetizers', 'Caesar Salad', 4.25);
menu.addDishToCourse('appetizers', 'fish', 5.25);
menu.addDishToCourse('appetizers', 'meat', 4.25);
menu.addDishToCourse('mains', 'Cat', 4.25);
menu.addDishToCourse('mains', 'dog', 5.25);
menu.addDishToCourse('mains', 'hamster', 2.25);
menu.addDishToCourse('desserts', 'lolly', 4.99);
menu.addDishToCourse('desserts', 'ice', 5.85);
menu.addDishToCourse('desserts', 'water', 4.00);

const meal = menu.generateRandomMeal();


  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.<anonymous> (/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)

in your case, _courses is not an object. Its a function:

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

trying to access a function with associative array notation, is not going to work.

Ahhhh that explains when i had the code in VSC those items were greyed out.
thanks for the swift reply it really has helped :+1: :+1: :+1:

when things like that (greyed out) happen, it means our editor is trying to tell us something. Always worth to investigate then

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.