I am trying to convert the JSON of an interface given by an API to different arrays which consist of dedicated objects. The type of the object is given as an variable in the interface.
The interface:
export interface Interface{
interfaceClassType: string;
}
I am accessing my json data like this:
getJSON(): Observable<Interface[]> {
return this.http.get<Interface[]>(URL)
.pipe(
retry(2),
catchError(this.handleError)
);
}
This method is called like this:
arrayWithObjects: Interface[];
class1Array: Class1[];
class2Array: Class2[];
processJSON(): void {
this.configService.getJSON().subscribe(results => this.arrayWithObjects = results);
this.arrayWithObjects.forEach (function (object) {
switch (object.interfaceClassType) {
case "first":
this.class1Array.push(object as Class1);
break;
case "second":
this.class2Array.push(object as Class2);
break;
}
}.bind(this))
}
I get the same result when i call it like this:
arrayWithObjects: Interface[];
class1Array: Class1[];
class2Array: Class2[];
processJSON(): void {
this.configService.getJSON().subscribe(results => this.arrayWithObjects = results);
this.arrayWithObjects.forEach ((object) => {
switch (object.interfaceClassType) {
case "first":
this.class1Array.push(object as Class1);
break;
case "second":
this.class2Array.push(object as Class2);
break;
}
})
}
When I execute this methods i always get an error saying: ERROR TypeError: this.class1Array is undefined