I have a Guard protecting a certain route. In the
canActive
1 canActivate(route: ActivatedRouteSnapshot,
2 state: RouterStateSnapshot) : Observable<boolean>|boolean {
3 return this.provider.getA().map(
4 dataA => {
5 return this.provider.getB().map(
6 dataB => {
7 return (dataB.allowed);
8 }
9 );
}
);
}
getA() : Observable<any> {
return this.http.post(URL,payload).
map(response => response.json());
Observable<Observable<boolean>>' is not assignable
to type 'Observable<boolean>'
You should use switchMap
here:
return this.provider.getA().switchMap(
dataA => {
return this.provider.getB().map(
dataB => {
return (dataB.allowed);
}
);
}
);
In your code, you are creating an Observable of Observables. switchMap
flattens that structure and emits the emitted items of B through A.