Search code examples
angularrxjsangular-http-interceptors

Angular Http Interceptor - fromPromise error - TypeError: (0 , x.fromPromise) is not a function


I am use the function below to attach recaptcha v3 to Http Request.

@Injectable()
export class RecaptchaInterceptor implements HttpInterceptor {
constructor(private recaptchaService: ReCaptchaService) { }

intercept(httpRequest: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {    
  return fromPromise(this.recaptchaService.execute({ action: 'login' }))
    .pipe(switchMap(token => {
      const headers = httpRequest.headers
        .set('recaptcha', token)
      const reqClone = httpRequest.clone({
        headers
      });
      return next.handle(reqClone);
    }));
}

In localhost this is work fine, But from some reason after publish I am getting the following error:

Uncaught (in promise): TypeError: (0 , x.fromPromise) is not a function TypeError: (0 , x.fromPromise) is not a function

What can I try next?


Solution

  • You can use from instead of fromPromise because i think you are using rxjs 6+

    import { from } from 'rxjs';
    
    ...
    return from(this.recaptchaService.execute({ action: 'login' }))