Help needed Meal maker project

Hello I have an issuse with my code, I looked at it for the past 2 hours, rewrited the whole thing and i still get the same ERROR.
/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:36
return this._courses[courseName].push(dish);
^

TypeError: Cannot read property ‘appetizers’ of undefined
at Object.addDishToCourse (/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:36:25)
at Object. (/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)
This is my code :smile:

const menu ={

_course: {

  appetizers:[],

  mains:[],

  desserts:[]

},

get appetizers(){

  return this._course.appetizers

},

get mains(){

  return this._course.mains

},

get desserts(){

  return this._course.desserts

},

set appetizers(appetizers){

  this._course.appetizers =appetizers;

},

set mains(mains){

  this._course.mains =mains;

},set desserts(desserts){

  this._course.appetizers =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 appetizers = this.getRandomDishFromCourse('appetizers');

  const mains = this.getRandomDishFromCourse('mains');

  const desserts = this.getRandomDishFromCourse('desserts');

  const totalPrice = appetizer.price + main.price+ dessert.price;

  return `Your meal is ${appetizer.name},${main.name},${dessert.name},and the total price is${totalPrice}`

}

} ;

menu.addDishToCourse('appetizers', 'salad',4.0)

menu.addDishToCourse('appetizers', 'wings',4.0)

menu.addDishToCourse('appetizers', 'cocaine',100.0)

menu.addDishToCourse('mains', 'kfc',4.0)

menu.addDishToCourse('mains', 'mac',4.0)

menu.addDishToCourse('mains', 'donner',4.0)

menu.addDishToCourse('desserts', 'ciocolata',4.0)

menu.addDishToCourse('desserts', 'inghetata',4.0)

menu.addDishToCourse('desserts', 'eclere',4.0)

const meal = menu.generateRandomMeal();

console.log(meal)

lets insert a .log() to inspect the code:

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

we can see that this._courses gives an undefined? What could cause this error? Think about this for a second before continue reading the rest of this answer

Given it some thought? good. Lets say we have an object with a property:

const example = {
   a: 3,
}

and say we want to add a method to this object for a pretty print:

const example =  {
   a: 3,
   prettyPrint() {  
     console.log(`the value of a is: ${this.a}`);
  }
}

example.prettyPrint()

this gives us context (the current object). we can even see this if we like:

const example =  {
   a: 3,
   prettyPrint() {  
     console.log(this)
  }
}

example.prettyPrint()

however, your method is outside/after the object, which means this has a very different context

where should i insert the log? after the code or inside?

i get this error when I insert the log
/home/ccuser/workspace/learn-javascript-objects-meal-maker/app.js:50
console.log(courseName, this._courses)
^
SyntaxError: Unexpected token .
at createScript (vm.js:53:10)
at Object.runInThisContext (vm.js:95:10)
at Module._compile (module.js:543:28)
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)

can I see how and where you inserted the console log? Please post your full code

You have some inconsistencies when accessing the appetizers/mains/desserts arrays. In one place you have this._course.appetizers (which is correct in accordance with how you named the _course object. But in another place you are trying to access it with this._courses[courseName] (in addToCourse method for example and also in some other functions there is the same issue). You have _course and _courses (with an extra s). These two have to be the same in order to access the object holding the arrays.

Also, in the get courses() method you are accessing appetizers with this.appetizers. Here you don’t have the _course object included. You would need to change this to something like this._course.appetizers. So look over all these references and make sure they match and then troubleshoot the error messages if there are any remaining problems. For example, the specific error msg that you posted has the issue of using _courses with plural.

2 Likes

Thank you guys, it s all good now, that was the problem, yesterday I couldn t see it . I had some spelling mistakes on the _course and other id s.
Have a nice day !!

1 Like