I have a page that opens on such a routing commitments/:id.
And I want to display the dates of reports belonging to this particular page, by id. But I get an message that it is impossible to read the property "commitment_id":
ERROR TypeError: Cannot read property 'commitment_id' of undefined
at eval (commitment.component.ts:88)
at Array.filter ()
at SafeSubscriber.eval [as _next] (commitment.component.ts:87)
at SafeSubscriber.__tryOrUnsub (Subscriber.js:239)
at SafeSubscriber.next (Subscriber.js:186)
at Subscriber._next (Subscriber.js:127)
at Subscriber.next (Subscriber.js:91)
at CatchSubscriber.Subscriber._next (Subscriber.js:127)
at CatchSubscriber.Subscriber.next (Subscriber.js:91)
at MapSubscriber._next (map.js:85)
ts:
commitments: Array<Commitment>;
selectedCommitment = null;
reportingDates: Array<ReportingDate>;
filteredReportingDates = [];
id: number;
routeId: any;
public errorMsg;
ngOnInit() {
this.routeId = this.route.params.subscribe(
params => {
this.id = +params['id'];
}
)
let commitmentRequest = this.route.params
.flatMap((params: Params) =>
this.servCommitment.getCommitment(+params['id' ]));
commitmentRequest.subscribe(response => this.commitment = response.json(), error => this.errorMsg = error);
}
@Input() commitment: Commitment;
onSelectedReportingDate(commitmentId) {
this.selectedCommitment = this.commitments.find(
(el) => {
return el.commitment_id === commitmentId
}
);
this.servReportingDate.getReportingDates().subscribe(
reportingDate => {
this.reportingDates = reportingDate;
this.filteredReportingDates = this.reportingDates.filter(
(reportingDate) => reportingDate.l_commitments_id === this.selectedCommitment.commitment_id
);
}
);
}
html:
<div *ngIf="commitment">
<button type="submit" class="btn btn-info btn-simple" (click)="onSelectedReportingDate(commitment.commitment_id)">Test</button>
<div *ngIf="selectedLiability">
<div *ngFor="let reportingDate of filteredReportingDates">
{{ reportingDate.reportingdate_plan }}
</div>
</div>
</div>
line 87-88-89:
this.filteredReportingDates = this.reportingDates.filter(
(reportingDate) => reportingDate.l_commitments_id === this.selectedCommitment.commitment_id
);
commitment.component.ts:88. Highlight that also - Sivakumar Tadisetticommitment.component.ts:88- tommuellerthis.selectedCommitment = this.commitments.find(line. can you doconsole.log(this.selectedCommitment)after find? and check the console - Sivakumar Tadisettithis.commitments, so when trying to do afindfor that array,selectedCommitmentwould beundefinedthere and when you are on line 88 trying to read thecommitment_idofselectedCommitmentit'sundefined. - AJT82