After writing the route guard
as shown below, I encountered an issue with the else
statement that was not returning a result, even though it should have. Surprisingly, there were no errors either.
this.hotelSettingsService.get().pipe(map(res => {
if (res) {
By removing the
from(this.localStorageService.get(LocalStorage.SCROLL_VIEW_HOLDER)).pipe(map(path)
section, the code started working.
I suspect that I might have made a fundamental mistake. Any insights on this?
Check out a video demonstrating this behavior: Video
canActivate(): Observable<boolean> {
return from(this.localStorageService.get(LocalStorage.SCROLL_VIEW_HOLDER)).pipe(map(path => {
if (path) {
this.router.navigate(path);
return true;
} else {
this.hotelSettingsService.get().pipe(map(res => {
if (res) { // not come to here
this.router.navigate([this.getLandingPage(environment.hotelName)]);
return true;
}
}), catchError((err) => {
return of(false);
}));
}
}), catchError((err) => {
return of(false);
}));
}
Adding a return
seems to resolve the issue:
https://i.stack.imgur.com/hb1rq.png
This simplified version is functioning properly:
canActivate(): Observable<boolean> {
return this.hotelSettingsService.get().pipe(map(res => {
if (res) {
this.router.navigate([this.getLandingPage(environment.hotelName)]);
return true;
}
}), catchError((err) => {
return of(false);
}));
}
}