I want to return data from API in angular route resolver but if there is any error in API data due to stale token I would refresh the token again and hit the same API.Below is the code for it
import { CommonUtlitiesService } from './../services/commonutilities.service';
import { UserService } from './../services/user.service';
import { Injectable } from '@angular/core';
import { Resolve } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/delay';
@Injectable()
export class CurrentUserProfileResolver implements Resolve<any> {
constructor(private userservice:UserService,
private commonservice:CommonUtlitiesService) {}
resolve() {
return this.getactiveuserinfo();
}
getactiveuserinfo(maxcount=3){
//get user data
this.userservice.getactiveuseraccountData().subscribe(res => {
console.log(res);
return res;
},
(err) => {
//refresh stale token
this.commonservice.refreshToken().subscribe(res => {
maxcount--;
if (maxcount > 0) {
//again hit The API
this.getactiveuserinfo(maxcount - 1);
}
});
});
}
}
The service code for userservice is as below:
getactiveuseraccountData(){
return this.http.post(url, dataSend, options)
.map(res => res.json());
}
In my component I get data as:
let data= this.route.snapshot.data.userdata;
but this does not contain any API response.
If I change my code in resolver to direct hit the service it works fine. For example:
import { CommonUtlitiesService } from './../services/commonutilities.service';
import { UserService } from './../services/user.service';
import { Injectable } from '@angular/core';
import { Resolve } from '@angular/router';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/delay';
@Injectable()
export class CurrentUserProfileResolver implements Resolve<any> {
constructor(private userservice:UserService,
private commonservice:CommonUtlitiesService) {}
resolve() {
//this works fine
return userservice.getactiveuseraccountData()
}
help me figure out the issue