未捕获的类型错误:无法读取属性'name'即使它存在,也无法定义

Uncaught TypeError: Cannot read property 'name' of undefined even it exist

本文关键字:name 定义 存在 属性 类型 错误 读取      更新时间:2023-09-26

更新以简化问题:

通过字符串索引访问属性时出现问题,但使用密钥有效

for(var i in this.props.data[this.props.activeMenu]){
    console.log(i, this.props.data[this.props.activeMenu][i]);
}

输出:

url /api/timekeeping
name Timekeeping
submenus Object {dtr: Object, schedules: Object}
id timekeeping

但我在调用console.log(i, this.props.data[this.props.activeMenu]['name'])时出错。name是一个有效的密钥,因为它是在我检查密钥时显示的。

当我试图访问console.log(this.props.data['timekeeping']['name'])时,我收到了这个错误Uncaught TypeError:无法读取未定义的属性'name'

我认为您应该使用:console.log(this.props.data['timekeeping'].name).

试试看。