组件里Cannot read property 'length' of undefined

题目描述

在组件里Cannot read property 'length' of undefined找不到length

相关代码

// 请把代码文本粘贴到下方(请勿用图片代替代码)
1、 let index = this.$utils.findObjIndex(this.name, 'datum_customer_shareholder_id', p.datum_customer_shareholder_id)

          if (index > -1) {
            p.name = this.name[index].real_name
          }
          
          
          2、组件:export const findObjIndex = (array, attr, val) => {

for (let i = 0; i < array.length; i++) {

if (array[i][attr] === val) { return i }

}
return -1
}
3、
错误:
clipboard.png

相关代码图片:

clipboard.png

你期待的结果是什么?实际看到的错误信息又是什么?

答案

阅读 2.9k
1 个回答

this.name的值为undefined,执行this.name.length,抛出这个错误

在函数findObjIndex里做个数据监测,或者在函数使用时过滤下数据

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