I am facing difficulties when trying to push a value into an array. Below is the code snippet I am working on: My goal is to localize an Angular application, but the data needed for translation is not stored in the HTML file - it's in the ts
file.
export class EventReportModalComponent implements OnInit {
reasons: Array<string>;
reasonsKeys = [
'report.reason.nudity',
'report.reason.hate-speech',
'report.reason.violence',
'report.reason.bullying',
'report.reason.false-information',
'report.reason.undesirable-content',
'report.reason.other',
];
constructor(
private translate: TranslateService
) {
let reasons;
for (let item of this.reasonsKeys) {
this.translate.get(item).subscribe(res => {
reasons.push(res);
});
}
this.reasons = reasons;
}
ngOnInit(): void {
}
}
An error occurred:
TypeError: Cannot read property 'push' of undefined
I am confused about how to solve this issue in JavaScript. Can someone help me identify my mistake?