Meal Maker: Keeps on spitting out: Cannot read property 'length' of undefined

Every time I run this program, I get: Cannot read property ‘length’ of undefined

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
  },
  get appetizers() {
    return this._courses.appetizers;
  },
  set appetizers(appetizersIn) {
    this._courses.appetizers = appetizers;
  },
  get mains() { 
     return this._courses.mains;
  },
  set mains(mainsIn) {
     this._courses.mains = mains;
  },
  get desserts() {
    return this._courses.desserts;
  },
  set desserts(dessertsIn) {
     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('appetizer');
    const main = this.getRandomDishFromCourse('main');
    const dessert = this.getRandomDishFromCourse('dessert');
    const totalPrice = appetizer.price + mains.price + dessert.price;
    return `Your meal is ${appetizer.name}, ${main.name}, and ${dessert.name}, and the total price is ${totalPrice}.`;
  }
};

menu.addDishToCourse('appetizers', 'salad', 4.00);
menu.addDishToCourse('appetizers', 'wings', 4.50);
menu.addDishToCourse('appetizers', 'fries', 5.00);

menu.addDishToCourse('mains', 'steak', 10.25);
menu.addDishToCourse('mains', 'salmon', 7.75);
menu.addDishToCourse('mains', 'tofu', 11.20);

menu.addDishToCourse('desserts', 'ice cream', 3.00);
menu.addDishToCourse('desserts', 'coffee', 3.00);
menu.addDishToCourse('desserts', 'cake', 3.25);

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

This is the error I’m getting:

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:41
    const randomIndex = Math.floor(Math.random() * dishes.length);
                                                         ^

TypeError: Cannot read property 'length' of undefined
    at Object.getRandomDishFromCourse (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:41:58)
    at Object.generateRandomMeal (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:45:28)
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:65:17)
    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)

This is the Meal Maker Project, I even tried following the video they had on this project but i couldn’t figure anything out. Help please!

Hey there, @bionic9011! Welcome to the forums :smiley:

In your generateRandomMeal() function, when you call getRandomDishFromCourse(), you’re passing ‘appetizer’, ‘dessert’ and ‘main’ as the arguments. But you should be passing ‘appetizers’, ‘desserts’ and ‘mains’, since those are the actual names of the properties you’re trying to access.

Hope this helped :slight_smile:

2 Likes

When you’re calling getRandomDishFromCourse() here, you’re calling with the singulars of the courses e.g. appetizer. However your getter method in the menu object is set as appetizers, and so it’s not actually returning the array you are looking for! There’s a few instances throughout of singular and plural courses getting mixed up, so have a look through your code and see where each one is called, and what it should be referring to.

You may also want to check your setter methods as you do not appear to be using the parameter in the set functions.

EDIT: answer above just got in before me, but leaving my reply up for the rest of what I said also.

1 Like

Thanks for the help guys! Much appreciated :+1:.

1 Like