简体   繁体   中英

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)

在此处输入图片说明 https://jsfiddle.net/5wdb0eco/

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;
        };

ie 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);
                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM