Javascript Project: Meal Maker

Hello, I do not know what is wrong with my code.

const menu = {
  _courses: {
    appetizers: [],
    mains: [],
    desserts: [],
    get appetizers() {
      return this._courses.appetizers;
    set appetizers(appetizers) {
      this._courses.appetizers = appetizers;
    get mains() {
      return this._courses.mains;
    set mains(mains) {
        this._courses.mains = mains;
    get desserts() {
      return this._courses.desserts;
    set desserts(desserts) {
        this._courses.desserts = desserts;

  get courses() {
      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);

      let dishes = this._courses[courseName];
      let randomIndex = Math.floor(Math.random() * dishes.length);
      return dishes[randomIndex];



      const appetizer = this.getRandomDishFromCourse('appetizers');
      const main = this.getRandomDishFromCourse('main');
      const dessert = this.getRandomDishFromCourse('dessert');



  menu.addDishToCourse('dessert', 'Bingsu', 5.90);

This is the error in the console:

    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:41:37)
    at Object.<anonymous> (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:65:8)
    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)

Run your code with these consoles in the addDishToCourse method:


The first console logs the keys of the _courses object you want to push to.
The second logs the courseName you passed as an argument to the method.

That will help you to spot the error.

While you push to an array, you do not need to additionally return from the function.

“Cannot read property ‘push’ of undefined” Indicates that the “undefined” object doesn’t have a method called push. Stepping back slightly, this shows that " this._courses[courseName] " is returning undefined. since you call the method several times, you should use console.log (or other debugging tools) to identify:

  • Is it failing every time
  • If fit only fails some times, which data is causing it to fail. Look for which value might cause _courses[courseName] to be undefined.
1 Like

Hi, I wrote the script, but in console it says that the .length is undefined…
I keep rewriting the code, and it keeps generating erros.
I don’t what I am doing wrong…

Yeah, I’ve got the same problem. I ended up following the walkthrough step for step and still got the same error.

Hello, @jacquelineatsukotida, and welcome to the forums.

You need to observe what is happening with your code. You can make observations by printing things out. Try:

getRandomDishFromCourse(courseName) {
  console.log(courseName); //Add this line to print the parameter. Is it what you expected?
  const dishes = this._courses[courseName]; //Do you have a property of the _courses object with the exact name supplied to the parameter?

Here is an example that might help you understand the error better:

const pets= {
	"catNames":["name1", "name2"],
	"dogNames": ["name3", "name4"]

If you try to reference a value that isn’t part of the object, you will get undefined as the value:

const fish = pets["fishNames"];

In this case “fish” will be undefined as “fishNames” is not a valid value in the object.

If you try to use any array methods on “fish” you will get an error.

Here is more information about the undefined value: undefined - JavaScript | MDN .

Between making sure you understand the error, and adding logging as described above, you should be able to make progress.

Anyone who needs help, please use this as a reference and continue your learning journey! :slight_smile: