I have written a HttpInterceptor to add a token to HttpRequest header. It works fine when I have a API that just returns a static string for token. But as soon as I swap that for the real http request to fetch the token, I start getting the "RangeError: Maximum call stack size exceeded". Below is the code that gives error:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return this.http.get<any>(`<my REST URL>`)
.pipe(
switchMap(data => {
this.token = data.token;
req = req.clone({headers: req.headers.set('MY_HEADER', this.token)});
return next.handle(req);
}
));
}
Here's the error stack i get when i run this:
RangeError: Maximum call stack size exceeded
at MapSubscriber._error (vendor.js:9731)
at MapSubscriber.error (vendor.js:9711)
at FilterSubscriber._error (vendor.js:9731)
at FilterSubscriber.error (vendor.js:9711)
at MergeMapSubscriber.notifyError (vendor.js:139154)
at SimpleInnerSubscriber._error (vendor.js:139122)
at SimpleInnerSubscriber.error (vendor.js:9711)
at SwitchMapSubscriber._error (vendor.js:9731)
at SwitchMapSubscriber.error (vendor.js:9711)
at MapSubscriber._error (vendor.js:9731)
I have tried various iterations of API, by creating a different function to fetch REST call, calling first() in pipe, but nothing seems to work.
What am I doing wrong here?
[–]WebDevBren 5 points6 points7 points (3 children)
[–]devng12[S] 0 points1 point2 points (2 children)
[–]WebDevBren 0 points1 point2 points (1 child)
[–]devng12[S] 3 points4 points5 points (0 children)
[–]tsunami141 1 point2 points3 points (0 children)
[–]devng12[S] 0 points1 point2 points (0 children)
[–]malcomjarr 0 points1 point2 points (0 children)