报错Cannot read property 'length' of undefined ?

报错如下:
Uncaught TypeError: Cannot read property 'length' of undefined
请问是哪里出问题了, 该怎么解决, 谢谢
html


  changeIndex (index){
    console.log(index)
    this.activeIndex = index
    this.sportDatabaseOrderBy = this.sportDatabase.slice(0) // 数组深拷贝
    for (let i = 0; i < this.sportDatabaseOrderBy.length; i++) {
       let firstLetter = py.getFullChars(this.sportDatabaseOrderBy[i].name).slice(0,1)
       this.sportDatabaseOrderBy[i].firstLetter = firstLetter
    }
  }
阅读 80.1k
5 个回答

在for loop时, this.sportDatabaseOrderBy是undefined.

检查this.sportDatabaseOrderBy是否有值

你的this.sportDatabaseOrderBy是underfined,你检查下this.sportDatabase

在这钟报length错误的情况下,一般都是这个值是undefined,或是特殊情况下取不到

新手上路,请多包涵

当取this.sportDatabaseOrderBy的值为undefined时,length属性就取不到值了;
需要先进行判断,this.sportDatabaseOrderBy != undefined判断之后,再执行this.sportDatabaseOrderBy.length。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题