Error cannot read property "length" of undefined

0
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 allExpenses  = [];
	var allIncomes = [];
	var totalExpenses = 0;
	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 UIController = (function(){
	var Domstrings = {
		
		inputType: '.add__type',
		inputDescription: '.add__description',
		inputValue:'.add__value',
		inputButton:'.add__btn'
	};
	return {
		getInput: function(){
			return {
				type: document.querySelector(Domstrings.inputType).value,
				description: document.querySelector(Domstrings.inputDescription).value,
				value: document.querySelector(Domstrings.inputValue).value,
				//btn: document.querySelector(Domstrings.inputButton)
			};
		},
		getDOMstring: function(){
			return Domstrings;
			
		}
	};
	
})();

var controller  = (function(budgetCtrl,UICtrl){
	var setupEventListeners = function(){
		var input = UICtrl.getInput();
		document.querySelector(DOM.inputButton).addEventListener('click',ctrlAddItem);
		document.addEventListener('keypress',function(event){
		if (event.keyCode===13)
		{
			ctrlAddItem();
		}
			
		
	});
		
	};
	var DOM = UICtrl.getDOMstring();
	var ctrlAddItem = function(){
		var input, newItem;
		//wypelnienie inputów
		input = UICtrl.getInput();
		//dodanie itemow do controlera
		newItem =  budgetCtrl.addItem(input.type, input.description,input.value);
		//dodanie item do UI
		//calculate budget 
		//display budget on ui
		//console.log(input);
	};
	
	/*var z = budgetCtrl.publicTest(5);
	return {
		anotherPublic:function(){
			console.log(z);
			
		}
		
	}*/
	
	return {
		init:function(){
			
			console.log('Wystartowano');
			setupEventListeners();
		}
		
	};
	
	
})(budgetController,UIController);
controller.init();

Nie rozumiem, czemu parser przy linii:

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

Wypisuje mi błąd jak w temacie...??

0

Zatem najwyraźniej data.allItems[type] jest undefined - komputer nie próbuje Cię oszukać ;-p

Odpal sobie debugger i zobacz, co się tam kryje.

0

Mamy XXI wiek - nie stosuj dupa debugging, tylko odpal rzeczywisty debugger ;-)
Poczytaj o instrukcji debugger;.

0
  1. Doszedłem, że w kodzie miałem dobrze, ale w html się walnąłem.
  2. Debugger - oczywiście doczytam.

1 użytkowników online, w tym zalogowanych: 0, gości: 1