我是js的新手。
我在sportsRecord中获取值,但无法在tempObj['TigerNo']中分配值。
当我分配时,我得到了这个错误--
类型错误:无法读取未定义的属性“tigerno”
tempobj['tigerno']=this.getdefault(运动记录).tigerno;
我在下面提供相关代码和GIST中的全部代码
你们能告诉我怎么修理吗?
在下面提供我的代码
https://gist.github.com/niniyzni/5c19f385b35f9418086125799803efe7

TypeError: Cannot read property 'TigerNo' of undefined
    at playerLionUpdate.setNesetTigerstworks (player-LionUpdate.ts:980)
    at SafeSubscriber.eval [as _next] (player-LionUpdate.ts:367)


    getDefault(sportsRecord: any){
        for(var i=0; i<sportsRecord.length; i++){
            if(sportsRecord[i].hasOwnProperty("isDefault"))
            {
                if(sportsRecord[i].isDefault)
                    return sportsRecord[i];

                break;
            }
        }
    }

    setTigers(sportsRecord: any) {


        let that = this;
        let tempObj = {};
        //tempObj['TigerNo'] = 237;
        //tempObj['Lion'] = getDefault(playermoons).Lion

       tempObj['TigerNo'] = this.getDefault(sportsRecord).TigerNo;
        //let defauktTigerNOsportsRecord =  sportsRecord.find((elem) => { return elem.isDefault; });

       //
        tempObj['TigerNo'] = sportsRecord.playerTigers[0].TigerNo;
        tempObj['TigerName'] = 'Nail Sports Bay Area';
        // tempObj['TigerNo'] = sportsRecord.selectedTigerNo;
        // tempObj['TigerName'] = sportsRecord.selectedTigerName;
        this.TigerTempArray = [];
        this.TigerTempArray.push(tempObj);
        let TigerssportsRecord = sportsRecord.playerTigers;
        let TigerssportsRecordList = this.TigerTempArray;
        let selectedTiger = TigerssportsRecord.find(elem => elem.TigerNo == 237);
        this.playerLionUpdateVal.TigerNo = 237;
        // let selectedTiger = TigerssportsRecord.find(elem => elem.TigerNo == sportsRecord.selectedTigerNo);
        // this.LionCreateVal.TigerNo = sportsRecord.selectedTigerNo;
        this.selectedTigerArr = selectedTiger;
        this.TigerCarousel.setTigersportsRecord(TigerssportsRecord, TigerssportsRecordList, 237);
        //this.TigerCarousel.setTigersportsRecord(TigerssportsRecord, TigerssportsRecordList, sportsRecord.selectedTigerNo);
        $("#TigerCarouselLabel .TigerHint").css("display", "none");
        setTimeout(function () {
            $("#unSelectedTigersLogoBox1 .currentNwLogo").bind("click", function (e) {
                e.stopPropagation();
                that.singleSelection(sportsRecord, e);
            });
        }, 100);
    }


sportsRecord = {
        playerTigers:[
            {TigerNo: 237, TigerName: "Bird Bay Area", TigerkGroupNo: 1, isDefault: true, futureAccessDays: ""},
            {TigerkNo: 238, TigerName: "Bird California", TigerkGroupNo: 1, futureAccessDays: ""},
            {TigerkNo: 239, TigerName: "Bird Washington", TigerkGroupNo: 1, futureAccessDays: ""},
            {TigerNo: 240, TigerName: "Bird Philadelphia", TigerkGroupNo: 1, futureAccessDays: ""},
            {TigerNo: 241, TigerName: "TCN", TigerkGroupNo: 1, futureAccessDays: ""},
            {TigerNo: 242, TigerName: "Bird Chicago", TigerkGroupNo: 1, futureAccessDays: ""},
            {TigerNo: 243, TigerName: "Bird Boston", TigerkGroupNo: 1, futureAccessDays: ""},
            {TigerNo: 244, TigerName: "Bird Northwest", TigerkGroupNo: 1, futureAccessDays: ""}
        ]
}

最佳答案

变量:

sportsRecord.isDefault

设置不正确。
所以,
this.getDefault(sportsRecord)
退货
'undefined'

呼叫前设置sportsRecord.isDefault

08-08 03:52