`TypeError:无法读取未定义的属性'ballDepth'

时间:2019-07-30 22:05:31

标签: javascript reactjs redux

  • 我正在对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

2 个答案:

答案 0 :(得分:0)

if (ballValue.ballDepth)替换为if (ballValue && ballValue.ballDepth)

答案 1 :(得分:0)

可能是因为您的搜索功能未获得结果,所以您可以添加后续验证来对其进行评估。

示例:

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