I'm accessing this route fine:
http://localhost:8100/questions/question?id=3
Now I'm in trouble on how to handle two subscribers at the same time.
The first subscriber loads the questions array from the external service.
The second one gets the desired Question object according to the route param.
app_data:AppData;
question:Question;
ngOnInit() {
this.appService.app_data.subscribe((v) => { this.app_data = v; });
this.route.queryParams.subscribe(p => {
this.question = this.appService.app_data.questions.find(i => i.id === params.id);
});
}
The problem is that when I open this route, it tries to filter the array which is still not loaded by the service.
ERROR TypeError: Cannot read properties of undefined (reading 'find')
Am I doing something wrong?
Would suggest using
combineLatestrxjs operator to combine multiple observables and take the last emitted value.Demo @ StackBlitz
Bonus:
forkJoinaims to wait for all the Observables to be completed first then only emit their last emitted value for each observable. Howeverroute.params,route.queryParamsobservables will never be completed, thusforkJoinis not working in this scenario.