我有一个调用函数“ SaveNewOpportunity”的按钮。在组件文件中,我定义了功能。 但是,单击按钮时出现错误
ERROR TypeError: Cannot read property 'grower' of undefined
HTML代码:
<button
mat-flat-button
color="accent"
(click)="saveNewOpportunity()"
[disabled]="isEdit">
Save </button>
打字稿代码:-
saveNewOpportunity() {
this.opportunity.projectId = this.project.id;
this.opportunity.opportunityId = null;
this.opportunity.oppName = this.opportunity.oppName;
this.opportunity.description = this.opportunity.description;
this.opportunity.formula = this.opportunity.formula;
this.opportunity.createdDate = this.opportunity.createdDate;
this.opportunity.createdBy = sessionStorage.getItem('username');
this.opportunity.ptnlFormula = this.ptnl_formula;
this.opportunity.isonmain = this.opportunity.isonmain;
this.opportunity.active = this.opportunity.active;
this.opportunity.elgblFormula = this.eligible_formula;
this.opportunity.oppAction = this.opportunity.oppAction;
this.opportunity.flatCutOff = this.thresholdGroups.grower.cutOff;
this.opportunity.stopperCutOff = this.thresholdGroups.stopper.cutOff;
this.opportunity.dropperCutOff = this.thresholdGroups.dropper.cutOff;
// loader
this.isLoadingResults = true;
this.recommendationService
.saveNewOpportunity(this.opportunity)
.subscribe(
response => {
let newOppData: any = response;
if (this.opportunityList !== undefined) {
this.opportunityList.push(newOppData);
}
this.oppList = new MatTableDataSource<any>(
this.opportunityList
);
this.snakbar.statusBar(
'New Opportunity saved successfully',
'Success'
);
this.isOpportunityCreationScreen = true;
this.isLoadingResults = false; //loader
},
error => {
this.isLoadingResults = false; //loader
this.snakbar.statusBar(
'New Opportunity save failed',
'Failure'
);
}
);
}
我似乎找不到问题所在。浏览器控制台中显示的错误是
OpportunityComponent.html:734 ERROR TypeError: Cannot read property 'grower' of undefined
at OpportunityComponent.push../src/app/layout/opportunity/opportunity.component.ts.OpportunityComponent.saveNewOpportunity (opportunity.component.ts:772)
at Object.eval [as handleEvent] (OpportunityComponent.html:737)
at handleEvent (core.js:21652)
答案 0 :(得分:0)
按照@OleksiiMiroshnyk的建议 从此更改我的代码
thresholdGroups: ThresholdGroups
到
thresholdGroups: ThresholdGroups = new ThresholdGroups()
成功了。 尽管它起初可以工作,但我不知道发生了什么变化。 无论如何,创建对象实例如OleksiiMiroshnyk都建议我解决问题。