I am encountering an issue with my ngOnInit() method. The method fills a data list at the beginning and contains two different logic branches depending on whether there is a query param present (navigating back to the page) or it's the first opening of the page. However, I'm facing a strange error within these two logical branches.
Within the code below:
ngOnInit() {
this.route.queryParams.subscribe((queryParams: Params) => {
this.initFilters(queryParams);
if(this.group) {
this.getActiveGroups();
//console.log(this.activeGroupList);
}
});
this.getActiveGroups();
}
getActiveGroups(): void {
this.service123.getActiveGroups().subscribe((groupData: Group[]) => {
this.activeGroupList = groupData;
//console.log(this.activeGroupList);
})
}
The second comment (console.log line) successfully logs the data from the backend, demonstrating that the variable is defined. However, the first comment is causing an error by stating that the variable is undefined. Even though they are referencing the same variable, why is the first comment showing it as undefined? I require access to this list in the first commented part as well so that I can utilize it in functions. Should I define the variable as something like static?
How can I resolve this issue?