首页 > 解决方案 > `TypeError:无法读取未定义的属性'ballDepth'

问题描述

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

标签: javascriptreactjsredux

解决方案


可能是因为您的搜索功能没有得到结果,您可以添加后续验证来评估它。

例子:

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);
}

推荐阅读