I´ve got a Component which loads user data and should be able to handle direct accessing via url. My idea was to implement a resolver service in the router, which is allready working fine when accessing the component via routing through the application. But if I refresh the application on the url, the resolver service throws following error:
ERROR Error: "Uncaught (in promise): TypeError: this.userService.getCurrentUser() is undefined
It seems like the userService is not initialized fast enough when refreshing.
This is the resolver service:
@Injectable({
providedIn: 'root'
})
export class UserResolverService {
constructor(private userService: UserService, private authService: AuthService, private router:Router) { }
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<User> | Observable<never> {
let id = route.paramMap.get('id');
//return this.authService.checkToken().subscribe(res => {
if(this.authService.checkToken()){
return this.userService.getCurrentUser().pipe(take(1), mergeMap(user => {
if(user){
return of(user);
} else {
this.router.navigate['/login'];
return EMPTY;
}
}));
} else {
this.router.navigate['/login'];
return EMPTY;
}
}
}
This is the UserService method called:
getCurrentUser(): Observable<User>{
if(this.currentUser.value != null){
return this.currentUser.asObservable().pipe(filter(user => user != null));
} else {
this.setCurrentUser().subscribe(res => {
return this.currentUser.asObservable().pipe(filter(user => user != null));
})
}
The setCurrentUser() method get calls the backend with credentials to retrieve the user Data.
The contructor of the Component, which should be able to load its data when refreshing contains the following:
this.route.data.subscribe((data: {user: User}) => {
this.user = data.user;
this.initialDialogWindows();
this.initialViewGuidingVariables();
});
EDIT:
I changed it to the suggested solution of Aakash Garg. Now the ngOnit() methods only receive undefined and the resolver does not seem to wait for promises. Neither of the console logs beside "am I even here?" are executed.
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<User> {
let id = route.paramMap.get('id');
//return this.authService.checkToken().subscribe(res => {
if(this.authService.checkToken()){
console.log("am I even here?");
this.userService.getCurrentUserPromise().then(user => {
console.log("resolved user:" + user);
if(user){
return of(user);
} else {
console.log("navigating to login");
this.router.navigate['/login'];
return EMPTY;
}
});
} else {
console.log("navigating to login, because there is no token");
this.router.navigate['/login'];
return EMPTY;
}
}