I keep getting the error: "TypeError: Cannot read property 'push' of undefined"
The error is in this line: "this.courseName.push(dish);"
Here is the code:
let menu = {
_courses: {
_appetizers:[] ,
_mains:[],
_desserts: []
},
addDishToCourse(courseName, dishName, dishPrice){
const dish = { name: this.dishName, price: this.dishPrice };
// ERROR HERE: **this._courses[courseName].push(dish);**
},
};
I have been stuck with this error for hours. Is there something I'm not seeing?
Sorry if there is a simple solution to this. I'm relatively new to JavaScript.
Edit:
I was indeed calling the key incorrectly. I am now calling the function as:
menu.addDishToCourse("_appetizers", "random", 10);
console.log(menu._courses._appetizers);
When I log this in compiler it returns:
[ { name: undefined, price: undefined } ]
It seems like the function isn't getting called. Am I approaching this fundamentally incorrectly?
this._courses[courseName]is undefined. What is the value ofcourseName?dishwrong but there is no errors in console. Post how you call that function, you are most likely passing the wrong courseNamecourseNamedoes not seem to be present in_courses. Thats why it is undefined.keyname incourseName.