I'm trying to figure out how to return an observable to from the canActivate method in my guard, but I can't seem to figure it out.
I've read a bunch of answers regarding this, especially this one Angular2 - return boolean with subscribe to canActivate. I followed this answer pretty closely, but it still isn't working.
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
return this._adminService.maintenanceOptions('maint-options').map(data => {
if (data['maintSwitch'] == 'off') {
const navigationExtras: NavigationExtras = {
queryParams: { errorMessage: data['maintComment'] }
};
this.router.navigate(['/maintenance'], navigationExtras);
return false;
}
return true;
});
}
maintenanceOptions(apiPath: string) {
this.toggleLoading(true);
const url = this._baseUrl + apiPath;
const options = this._authService.getOptions();
return this.http.get(url, options)
.take(1)
.map((res: Response) => res.json())
.finally(() => this.toggleLoading(false));
}
I expected this to be able to accept the observable, but I'm making an error somewhere here. I keep getting this error in the chrome console:
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'length' of null
TypeError: Cannot read property 'length' of null at
HttpHeaders.push../node_modules/@angular/common/fesm5/http.js.HttpHeaders.applyUpdate
and then the error continues on and on.