I am working on an Angular component that utilizes a simple service to retrieve data based on an Enum parameter.
export enum AgeEnum {
UNDER_18 = 'UNDER_18',
ABOVE_18 = 'ABOVE_18'
}
Within my component, I have the following setup:
private ageArray = Object.keys(AgeEnum);
private restrictionsWrapper: RestrictionsWrapper;
constructor(private restrictionsService: RestrictionsService) {
}
ngOnInit(): void {
this.getRestrictionsForAge(this.ageArray);
}
private getRestrictionsForAge(ages: string[]) {
for (const age of ages) {
this.restrictionsService.getRestrictions(age as AgeEnum)
.subscribe((options) => {
this.restrictionsWrapper.restrictions = options.restrictions;
}, () => {
this.restrictionsWrapper.restrictions = null;
}
);
}
}
The UI Service method is defined as follows:
getRestrictions(age: AgeEnum): Observable<RestrictionsWrapper> {
const params = new HttpParams().set('age', age);
return this.http.get<RestrictionsWrapper>(BackendRoute.RESTRICTIONS, {params: params});
}
This is the model for RestrictionsWrapper
:
export interface RestrictionsWrapper {
restrictionDocuments: string[];
blocked: boolean;
}
The goal is to load different sets of restrictions based on the age, without duplicating methods. However, there seems to be an error:
Unhandled error occured. TypeError: Cannot set property 'restrictions' of undefined
Any suggestions on what might be incorrect or if using
this.restrictionsService.getRestrictions(age as AgeEnum)
is the best approach?