gpt4 book ai didi

javascript - 未捕获的类型错误 : Cannot read property 'length' of undefined. ..?

转载 作者:行者123 更新时间:2023-11-30 14:26:01 32 4
gpt4 key购买 nike

我意识到有人问了这个问题。但我不确定为什么我仍然收到此错误。

Ucaught TypeError: Cannot read property 'length' of undefined
at Object.addItem

我没有看到长度是如何未定义的,当我检查它是否大于 0 时,我是否没有正确访问 data 对象?

我有一个预算 Controller IIFE,它有一个返回 addItem 函数。在这个函数中,我在这一行得到一个错误:

     if (data.allItems[type].length > 0) {

这是完整的功能。

// BUDGET CONTROLLER
var budgetController = (function() {

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

var data = {
allItems: {
exp: [],
inc: []
},

totals: {
exp: 0,
inc: 0
}
};

return {
addItem: function(type, des, val) {
var newItem, ID;

//[1 2 3 4 5], next ID = 6
//[1 2 4 6 8], next ID = 9
// ID = last ID + 1

// Create new ID
if (data.allItems[type].length > 0) {
ID = data.allItems[type][data.allItems[type].length - 1].id + 1;
} else {
ID = 0;
}

// Create new item based on 'inc' or 'exp' type
if (type === 'exp') {
newItem = new Expense(ID, des, val);
} else if (type === 'inc') {
newItem = new Income(ID, des, val);
}

// Push it into our data structure
data.allItems[type].push(newItem);

// Return the new element
return newItem;
},

testing: function() {
console.log(data);
}
}

})();

我这里也是这么叫的。

var ctrlAddItem = function() {
var input, newItem;

// 1. Get the FIELD input data
input = UICtrl.getinput();

// 2. Add the item to the budget controler
newItem = budgetCtrl.addItem(input.type, input.description, input.value);

// 3. add the item to the UI

// 4. Calculate the budget

// 5. Display the budget on the UI


}

UI Controller - 我获取输入的地方。

// UI CONTROLLER
var UIController = (function() {
// some code
var DOMstrings = {
inputType: '.add__type',
inputDescription: '.add__description',
inputValue: '.add__value',
inputBtn: '.add__btn'
}

return {
getinput: function() {
// will be either inc or exp
return {
type: document.querySelector(DOMstrings.inputType).value,
description: document.querySelector(DOMstrings.inputDescription).value,
value: document.querySelector(DOMstrings.inputValue).value
}
},

getDOMstrings: function() {
return DOMstrings;
}
}
})();




// GLOBAL APP CONTROLLER
var controller = (function(budgetCtrl, UICtrl) {

var setupEventListerners = function() {
var DOM = UICtrl.getDOMstrings();
document.querySelector(DOM.inputBtn).addEventListener('click', ctrlAddItem);

document.addEventListener('keypress', function(event) {
if(event.keyCode == 13 || event.which == 13) {
ctrlAddItem();
}
});
};

var ctrlAddItem = function() {
var input, newItem;

// 1. Get the FIELD input data
input = UICtrl.getinput();

// 2. Add the item to the budget controler
newItem = budgetCtrl.addItem(input.type, input.description, input.value);

// 3. add the item to the UI

// 4. Calculate the budget

// 5. Display the budget on the UI


}

return {
init: function() {
console.log('application start');
setupEventListerners();
}
}

})(budgetController, UIController);


controller.init();

有什么想法吗?

最佳答案

看起来变量 input(来自 input = UICtrl.getinput())有一个属性 .type 不是expinc,或者没有 .type(即 undefined)。

这会导致 data.allItems[type]undefined,并且 undefined 没有 length.

关于javascript - 未捕获的类型错误 : Cannot read property 'length' of undefined. ..?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51923972/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com