【问题标题】:Angular Http Interceptor - fromPromise error - TypeError: (0 , x.fromPromise) is not a functionAngular Http Interceptor - fromPromise 错误 - TypeError: (0 , x.fromPromise) 不是函数
【发布时间】:2021-10-26 19:46:58
【问题描述】:

我正在使用下面的函数将 recaptcha v3 附加到 Http 请求。

@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);
    }));
}

在 localhost 中可以正常工作,但由于某些原因在发布后我收到以下错误:

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

接下来我可以尝试什么?

【问题讨论】:

    标签: angular rxjs angular-http-interceptors


    【解决方案1】:

    你可以使用 from 而不是 fromPromise 因为我认为你使用的是 rxjs 6+

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

    【讨论】:

    • 嗯,确实解决了问题,谢谢!你知道解释为什么吗?它们之间有什么不同?
    • 没有差异,fromPromise 在 rxjs 6 中被替换为 from。fromPromise 不再是其 APi 的一部分
    • 好的,知道了。谢谢!
    猜你喜欢
    • 2016-07-11
    • 2015-06-26
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 2021-07-16
    • 2022-01-08
    • 2021-06-30
    • 2020-08-04
    相关资源
    最近更新 更多