I need to do a http request inside my interceptor.
I try:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const authToken = localStorage.getItem('token');
if(authToken != null){
if(req.url !== AppSettings.API_ENDPOINT + 'plano/busca'){
let http: HttpClient = this.injector.get(HttpClient);
const httpOptions = {
headers: new HttpHeaders({
'token': authToken,
'Authorization': 'bearer ' + authToken
})
};
return http.get(AppSettings.API_ENDPOINT + 'plano/busca', httpOptions).pipe(switchMap((response) => {
console.log(response);
const newRequest = req.clone({
setHeaders: { Authorization: `bearer ${authToken}`, token: `${authToken}` }
});
return next.handle(newRequest);
}),
catchError((err) => {
console.log(err)
return Observable.throw(err);
}
)
);
}}else {
return next.handle(req);
}
}
but i'm receiving in console:
TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable. at subscribeTo (subscribeTo.js:41) at subscribeToResult (subscribeToResult.js:6) at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._innerSub (mergeMap.js:70) at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._tryNext (mergeMap.js:67) at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber._next (mergeMap.js:50)
Comments
Post a Comment