Dear all; I was not sure if I should also share my html project. Im stuck here :/ I have checked few questions similar to that but no solution yet...
I wanted to make ID which should be different always but could not end up my problem.
Uncaught TypeError: Cannot read property 'length' of undefined Please help me about it : Uncaught
TypeError: Cannot read property 'length' of undefined at Object.addItem (app.js:30) at kontrolEkle (app.js:112) at HTMLDocument. (app.js:97)
//Price Budget Control var butcekontrol = (function(){
var Giderler = function(id,aciklama,deger){
this.id=id;
this.aciklama=aciklama;
this.deger=deger;
};
var Gelirler = function(id,aciklama,deger){
this.id=id;
this.aciklama=aciklama;
this.deger=deger;
};
var data = {
tumislem:{
gelir: [],
gider: []
},
toplam: {
gelir:0,
gider:0
}
}
when I return here :
return{
addItem: function(type,des,val){
var newItem,ID;
// YENİ ID OLUŞTURMA
if (data.tumislem[type].length > 0) {
ID = data.tumislem[type][data.tumislem[type].length - 1].id + 1;
} else {
ID = 0
}
// Tipine Göre Ekleme Çıkarma Yapma
if(type==='exp'){
newItem = new Giderler(ID , des , val)
} else if(type==='inc'){
newItem = new Gelirler(ID , des, val)
}
// Dataya Push İşlemi Yapma
data.tumislem[type].push(newItem);
// RETURN//
return newItem;
},
testing:function(){
console.log(data);}
};
})();
User Interface Control Code Area
var arayuz = (function(){
var Domstrings = {
InputType:'.add__type',
InputDescription:'.add__description',
InputValue:'.add__value',
InputBtn:'.add__btn',
};
return {
getInput: function() {
return {
type: document.querySelector(Domstrings.InputType).value, // Will be either inc or exp
description: document.querySelector(Domstrings.InputDescription).value,
value: document.querySelector(Domstrings.InputValue).value
};
},
getDomstrings:function(){
return Domstrings;
}
}
})();
And last part
var kontrol = (function(butceknt,arayz){
var setupcontroller = function(){
//KODLAR BURAYA
var DOM = arayuz.getDomstrings();
document.querySelector(DOM.InputBtn).addEventListener('click',kontrolEkle);
document.addEventListener('keypress', function(tik){
if(tik.keyCode===13){
kontrolEkle();
}})
//END Of the line
}
var kontrolEkle = function(){
//1. Formlardan Değer Alma
var input,addItem;
input = arayuz.getInput();
console.log(input);
//2.Bütçe Kontrole Ekleme
addItem = butcekontrol.addItem(input.type,input.description,input.value);
//3.Kullanıcı Arayüzünde Güncelleme
//4.Bütçeyi Hesaplama
//5.Kullanıcıya Son Bütçeyi Gösterme
}
return{
init:function(){
console.log('Application Started');
setupcontroller();
}
}
})(butcekontrol,arayuz);
kontrol.init();
发生这种情况是因为你试图获取 undefined 的长度,你能确保 data.tumislem[type].length 返回值,通过 console.log 吗?
Hey man I think this line is the issue.
ID = data.tumislem[type][data.tumislem[type].length - 1].id + 1;
data.tumislem[type]
is coming back as null
or undefined
, check there first.
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.