无法获取数组的长度

时间:2020-08-25 02:28:02

标签: javascript arrays

我正在尝试创建预算控制器。
当我尝试将项目推入数组时,出现此错误:

未捕获的TypeError:无法读取未定义的属性'length'

错误在第33行开始,

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

怎么了?

这是完整的代码:

var budgetController = (function(){

//function constructor
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;
        //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 off of inc or exp type
        if(type === 'exp'){
            newItem = new Expense(ID, des, val);
        } else if(type === 'inc'){
            newItem = new Income(ID, des, val);
        }

        //push onto data structure
        data.allItems[type].push(newItem);

        //return the new element
        return newItem;
    },

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

2 个答案:

答案 0 :(得分:4)

可能wait.Until()未定义,因为您将变量driver.Dispose()与未定义的属性一起使用。只需在此行之前添加data.allItems[type],然后查找输出即可。

答案 1 :(得分:1)

首先检查data.allItems[type]是否存在。

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

看来budgetController的内部结构实际上取决于这样的事实,即参数a)是否存在以及b)是否具有某种类型(或值)才能继续正常运行。

在这种情况下,您可能要防止允许budgetController继续进行,如果提供的参数意外,则会进行一些错误处理以停止执行。

var budgetController = (function() {

  //function constructor
  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) {
      if (!Object.keys(data.allItems).includes(type)) {
        throw new Error(`Whoops! type must be one of ${Object.keys(data.allItems).join(', ')}`)
      }
      if (!des) {
        throw new Error(`Whoops! des is required`)
      }
      if (!val) {
        throw new Error(`Whoops! val is required`)
      }
      var newItem, ID;
      //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 off of inc or exp type
      if (type === 'exp') {
        newItem = new Expense(ID, des, val);
      } else if (type === 'inc') {
        newItem = new Income(ID, des, val);
      }

      //push onto data structure
      data.allItems[type].push(newItem);

      //return the new element
      return newItem;
    },

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


var myBudgetController = budgetController
myBudgetController.addItem('unacceptable type value', 'desc', 1)