【问题标题】:When I try to make ID ; "Cannot read property 'length' of undefined."当我尝试制作 ID 时; “无法读取未定义的属性‘长度’。”
【发布时间】:2019-11-27 04:36:11
【问题描述】:

亲爱的;我不确定我是否也应该分享我的 html 项目。我被困在这里:/ 我检查了几个类似的问题,但还没有解决方案......

我想制作应该总是不同的 ID,但不能解决我的问题。

未捕获的类型错误:无法读取未定义的属性“长度” 请帮助我:未捕获

TypeError:无法读取未定义的属性“长度” 在 Object.addItem (app.js:30) 在 kontrolEkle (app.js:112) 在 HTML 文档。 (app.js:97)

//价格预算控制 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
       }
   }

当我回到这里时:

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

})();

用户界面控制代码区

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

     }

})();

最后一部分

    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();

【问题讨论】:

    标签: javascript


    【解决方案1】:

    这是因为您尝试获取未定义的长度,您可以通过console.log确保data.tumislem[type].length返回值吗?

    【讨论】:

    • console.log(data.tumislem[type])
    【解决方案2】:

    嘿,伙计,我认为这条线是问题所在。

    ID = data.tumislem[type][data.tumislem[type].length - 1].id + 1;

    data.tumislem[type] 将以nullundefined 的身份返回,请先检查那里。

    【讨论】:

      猜你喜欢
      • 2019-08-08
      • 1970-01-01
      • 2011-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-24
      • 1970-01-01
      • 2019-01-27
      相关资源
      最近更新 更多