APP_INITIALIZER runs a series of nested promises (I've tried subscribes with no difference in the result).
The APP_INITIALIZER needs to get authenticated before it goes to retrieve data from the API server. It also needs to pull two tables from the API server (in sequence).
In api.service, http/get authorization happen in a promise. After the promise (then), I go to get data from the API service.
The issue is the component ngOnInit() - It attempts to get the variables before they exist.
I have tried the following code in the component, but all that does is call the initData() twice.
this.people = await this.apiService.initData();
api.service:
async initData(): Promise<Person[]> {
this.userData$ = this.http.get('/.auth/me', {observe: 'response'});
this.userData$.toPromise().then( res => {
this.resBody = res.body;
this.token = res.body[0].access_token;
this.getLegalSub(this.legalsubdeptsURL)
.toPromise().then(legalsubdepts => {
this.legalsubdepts = legalsubdepts;
this.getPeopleData(this.personURL)
.toPromise().then(people => {
this.people = people;
return this.people;
});
});
});
}
return this.people;
}
app.module
export function initData(appInitService: APIService) {
return (): Promise<any> => {
return appInitService.initData();
}
}
...
providers: [
APIService,
{ provide: APP_INITIALIZER, useFactory: initData, deps: [APIService], multi: true }
],
the component that runs before APP_INITIALIZER is done
ngOnInit() {
this.people = this.apiService.people;
this.userName = this.apiService.userName;
console.log("username");
console.log(this.userName);
}
I need to get the authorization before I can get data from the API server. I then need the data from the API server before I process the component.
I eventually get data, but not in time for the component.