Meal Maker dishes.length undefined?

Hi y’all, I’ve been beating my head against this with no luck so hopefully someone can spot what I’m doing wrong? Code:

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

menu.addDishToCourse('appetizers', 'Manapua', 3.99);
menu.addDishToCourse('mains', 'fried rice', 12);
menu.addDishToCourse('desserts', 'ice cream', 6);

menu.addDishToCourse('appetizers', 'ladyfingers', 7.99);
menu.addDishToCourse('mains', 'Rice cake', 7.3);
menu.addDishToCourse('desserts', 'rice crispy treat', 2.99);

menu.addDishToCourse('appetizers', 'Fries', 7.99);
menu.addDishToCourse('mains', 'broccoli head', 7.3);
menu.addDishToCourse('desserts', 'Volcano cake', 2.99);

const meal = menu.generateRandomMeal();

console.log(meal)

//console.log(Object.values(menu._courses))

The error I’m getting is this:

/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:42
    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:42:58)
    at Object.generateRandomMeal (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:46:28)
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:66:19)
    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)

I’ve found a couple of other topics around this (or a similar) issue, but it’s hard to tell where/how exactly I’ve gone wrong. help?

this.courses (the line before it) doesn’t seem to exist. Did you mean this._courses (as you’ve referenced a bunch of times above)?

1 Like

Aha! That did fix that problem but then it sent up another error but one that was much easier to solve. Thanks!