Meal Maker project error (.push is not a function)

Hi, i’m having trouble finding out what i am doing wrong here, since i’ve pretty much copied what’s in the hint of the project.

Here’s my code:

const menu = {
    
    _courses: {
        appetizers: '',
        mains: '',
        desserts: ''
    },

    
    get appetizers () {
        return this._courses.appetizers;
    },

    set appetizers (newAppetizer) {
        if(typeof newAppetizer === 'string'){
            this._courses.appetizers = newAppetizer;
        }
        else{
            console.log('Please enter a valid string.');
        }
    },

    get mains () {
        return this._courses.mains;
    },

    set mains (newMains) {
        if(typeof newMains === 'string'){
            this._courses.mains = newMains;
        }
        else{
            console.log('Please enter a valid string.');
        }
    },

    get desserts () {
        return this._courses.desserts;
    },

    set desserts (newDessert) {
        if(typeof newDessert === 'string'){
            this._courses.desserts = newDessert;
        }
        else{
            console.log('Please enter a valid string.');
        }
    },

    get courses () {
       return this._courses;
    },

    addDishToCourse (courseName, dishName, dishPrice) {
        const dish = {
            name: dishName,
            price: dishPrice
        };
        
        this._courses[courseName].push(dish);
    },
};

But when i try to run the addDishToCourse method:

menu.addDishToCourse('desserts', 'ice cream', 2.99);

It returns this error:

this._courses[courseName].push(dish);
                                  ^

TypeError: this._courses[courseName].push is not a function
    at Object.addDishToCourse

Any help will be appreciated.

What type of variable can .push() be used on? What are you currently trying to use it on? Perhaps use a console.log() to see what you are trying to push to.

1 Like

Ok, so i figured it out.

I was declaring the _courses properties as empty strings, not empty arrays.

So when the .push() (array method) was called, it couldn’t change the values.

The correction i’ve made:

_courses: {
        appetizers: [],
        mains: [],
        desserts: []
    },
1 Like