Dwayne
Dwayne

Reputation: 179

Uncaught TypeError: Cannot read property 'replace' of undefine

I get the error once I enter details in the description and value section and hit the green arrow. Can anyone have a look what is going wrong at my end! Please refer to the below link https://jsfiddle.net/5wdb0eco/

Error message:

app.js:86 Uncaught TypeError: Cannot read property 'replace' of undefined
    at Object.addListItem (app.js:86)
    at HTMLButtonElement.ctrlAddItem (app.js:124)

enter image description here https://jsfiddle.net/5wdb0eco/

Upvotes: 0

Views: 415

Answers (1)

SKeney
SKeney

Reputation: 2301

Take a look at the code below:

        var ctrlAddItem = function(){
            var input, newItem;
            //  1. GET THE FIELD INPUT DATA
            input = UIController.getInput();
            console.log(input.type)
            //console.log(input);
            //  2. ADD THE ITEM TO THE BUDGET CONTROLLER
            newItem = budgetCtrl.addItem(input.type, input.description, input.value);
            console.log(newItem)
            //  3. ADD THE ITEM TO THE UI
            UICtrl.addListItem(newItem, newItem.type);

            //  4. CALCULATE BUDGET
            //  5. DISPLAY THE BUDGET ON THE UI
        };

I added the console.log but your newItem from budgetCtrl.addItem does not return an object with type property attached to it. Because of that passing it in to the UICtrl.addListItem function results in undefined

Your newItem that you are returning is one of the following:

        var Expense = function(id, description, value){
            this.id = id;
            this.description = description;
            this.value = value;
        };
        var Income = function(id, description, value){
            this.id = id;
            this.description = description;
            this.value = value;
        };

i.e. new Expense or new Income inside your addListItem

Change your expense and income functions to pass in a parameter of type and then in the addItem function pass the type into the instance as shown below:

                if(type === 'exp'){
                    newItem = new Expense(type, ID, des, val);
                }
                else if(type === 'inc') {
                    newItem = new Income(type, ID, des, val);
                }

Upvotes: 1

Related Questions