javascript - `TypeError:无法读取未定义的属性'ballDepth'
问题描述
- 我正在查找我的animalId,但出现错误
TypeError: Cannot read property 'ballDepth' of undefined
- 我在控制台中调试并发现我正在获取值。
if (ballValue.ballDepth) {
console.log('Sports this ballValue', ballValue.ballDepth);
}
- 所以我在上面使用了 if 条件,但我仍然遇到错误。- 你能告诉我如何解决它,并在下面提供我的相关代码片段
worldCardList.js
case 'Sports':
console.log(
' Sports this.props.crucialData--->',
this.props.crucialData
);
console.log(
' Sports this.props.playerIDs--->',
this.props.playerIDs
);
filteredResult = this.props.crucialData
.filter(
search =>
search.tiger === 'Sports' &&
search.lion === this.props.data.lion
)
.map(search => {
console.log(
' Sports search-->',
search.animalId
);
let ballValue = this.props.playerIDs.find(
playerID => playerID.animalId == search.animalId
);
if (ballValue.ballDepth) {
console.log('Sports this ballValue', ballValue.ballDepth);
}
return {
label: `${search.displayName}
| ${search.tiger}
| ${search.lion}
| ${search.playerIDs[0].number}
`,
value: search.lion,
checked: true,
};
});
this.setState({ groupcrucialData: filteredResult });
this.setState({ groupRadioValue: 'PRO' });
this.props.fetchanimalworldCardListByWolfno(
data.animalId,
'false',
this.getcrucialData
);
break;
输出
Sports this.props.crucialData--->
(32) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0:
tiger: "Sports"
displayName: "Height"
playerIDs: [{…}]
formattedlion: "12345"
includeSelected: false
licenseNumbers: (2) [{…}, {…}]
loadTime: "2018-07-21 06:08:05"
lowerOrgName: "Height"
yuyuyuNumbers: [{…}]
organizationName: "Height"
payloadId: 2323232323
animalId: 23232323232
lion: "8547845784578"
lionType: "EIN"
transactionId: "23323232323"
__proto__: Object
1:
tiger: "Sports"
displayName: "Height"
playerIDs: [{…}]
formattedlion: "12345"
includeSelected: false
licenseNumbers: [{…}]
loadTime: "2018-07-21 06:08:05"
lowerOrgName: "Height"
yuyuyuNumbers: [{…}]
organizationName: "Height"
payloadId: 232323433335
animalId: 9798667
lion: "8547845784578"
lionType: "EIN"
transactionId: "153216069365300"
__proto__: Object
Sports this.props.playerIDs--->
(20) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0:
playerID: "hj123hj12"
playerIDExpDate: "9999-12-31"
networks: "4784378347834783478"
yuyuyuNumbers: "58945894589"
animalId: 9798667
animalName: "Height"
animalType: "Sports"
ballDepth: "94059045904590459045"
__proto__: Object
1:
playerID: "232323"
playerIDExpDate: "9999-12-31"
networks: "4784378347834783478"
yuyuyuNumbers: "58945894589"
animalId: 23232323232
animalName: "Height"
animalType: "Sports"
ballDepth: "94059045904590459045"
__proto__: Object
Sports this ballValue 94059045904590459045
解决方案
可能是因为您的搜索功能没有得到结果,您可以添加后续验证来评估它。
例子:
const { playerIDs } = this.props; // Better to read
let ballValue = playerIDs.find(
playerID => playerID.animalId == search.animalId
);
// Here we check if there's any data on ballValue
if (ballValue && ballValue.ballDepth) {
console.log('Sports this ballValue', ballValue.ballDepth);
}
推荐阅读
- c# - 如何将 List 方法更改为 Array 方法?
- java - Spring Boot - 如何使用 cors 和 spring security 修复 Session bean
- ios - WebRTC iOS 13 问题与 Metal
- java - 如何在 Java 中截断部分字符串?
- amazon-web-services - 需要移动已部署的文件夹 - AWS CodeDeploy
- vectorization - 只有在使用 AVX 并链接到其他代码时,我才会遇到分段错误
- angular - 单击按钮时触发角管道事件
- python-3.x - 无法使用 python 3.7 运行带有 Tornado 事件循环的 ZMQStream
- powershell - 如何在PowerShell中将Localtime转换为纪元时间戳
- android - 使用 Ionic 更改文件权限