在我们的代码中,我有两个语句
const { column, showTooltip, tooltipValue, data } = props;
const key = column.bindProperties[0].properties[0].name;
在测试中,这给出了错误 “ TypeError:无法读取未定义的属性'0'。”
“ column.bindProperties [0] .properties [0] .name;”的含义是什么?以及如何测试。
在我们的代码中,我有两个语句
const { column, showTooltip, tooltipValue, data } = props;
const key = column.bindProperties[0].properties[0].name;
在测试中,这给出了错误 “ TypeError:无法读取未定义的属性'0'。”
“ column.bindProperties [0] .properties [0] .name;”的含义是什么?以及如何测试。
在JS中,您不能保证对象具有某些属性。
When you try to access
column.bindProperties[0].properties[0].name
, eithercolumn.bindProperties
orcolumn.bindProperties[0].properties
isundefined
- hence the error you're getting.You can either use
lodash
's_.get()
or validate the keys are defined using the redundantly annoying:This will make sure your code won't break. If one expression in the chain isn't defined, the expression will stop evaluating and you'll just get
undefined
as the result.