Abel Jojo
Abel Jojo

Reputation: 758

Angular resolve Rxjs map and subscribe multiple APIs

Application need to fetch the jwt token first and then using token, collect portal information. This code is written in resolver.

This code make an error exception :

Type 'Subscription' is not assignable to type 'Observable'

When Observable<any> is removed, resolve data is not available in component and it initiates components before resolve completes

@Injectable()
export class PortalLoadingResolverService implements Resolve<any> {
    constructor(
      private authService:AuthService,
      private emiDataService:EmiDataService,
    ) { }

    resolve():Observable<any> {
      return this.authService.getJwtToken()
      .map(res => res.json())
      .subscribe(JwtToken => {
        debugger;
        localStorage.setItem('id_token',JwtToken.token);
        return this.emiDataService.getBasicLoadingDatas();
      });
    }
}

Upvotes: 2

Views: 233

Answers (1)

Pankaj Parkar
Pankaj Parkar

Reputation: 136134

Your method isn't returning Observable here, that is what Typescript is yelling. Because once you applied subscribe method over Observable it return Subscription of observable. You could easily return a Observable by chaining Observable with switchMap operator.

resolve():Observable<any> {
  return this.authService.getJwtToken()
  .map(res => res.json())
  .switchMap(JwtToken => {
    localStorage.setItem('id_token',JwtToken.token);
    return this.emiDataService.getBasicLoadingDatas();
  });
}

Upvotes: 1

Related Questions