HttpInterceptor 在拦截器中调用另一个服务并等待响应

HttpInterceptor calling another service inside an interceptor and waiting for response

如果满足特定条件,我必须在拦截器中调用另一个服务,并且我希望原始服务等到新服务完成。我的第一个解决方案是使用 tap:

intercept(req: HttpRequest<any>, options: any, next: HttpServiceHandler)
  : Observable <HttpEvent<any>> {
    return next.handle(req, options).pipe(
      tap((event: HttpEvent<any>) => {
        if (event instanceof HttpResponse && event.ok && event.body) {
          const productsToGet = [...]
          if (* condition *) {
            const resourceService = this.injector.get(ResourceService);
            return resourceService.getProducts(productsToGet);
          }
        }
      })
    );
  }

但它显然没有像我想要的那样工作,因为原始方法在 getProducts 完成之前完成。然后我使用这个 thread 的解决方案和 switchMap:

return next.handle(req, options).pipe(
  filter((event: HttpEvent<any>) => (event instanceof HttpResponse && event.ok && event.body)),
  switchMap((event: HttpResponse<any>) => {
    const productsToGet = [...]
    if (* condition *) {
      const resourceService = this.injector.get(ResourceService);
      return resourceService.getProducts(productsToGet).pipe(
        mapTo(event)
      )
    } else {
      return of(event);
    }
  })
);

但现在我收到一个错误:

Uncaught (in promise) EmptyError: no elements in sequence.

你能告诉我我的解决方案是否有效吗?我应该怎么做才能让它发挥作用?

完整的错误堆栈:

> Error: Uncaught (in promise): EmptyError: no elements in sequence
    at resolvePromise (zone.js:852)
    at resolvePromise (zone.js:809)
    at zone.js:913
    at ZoneDelegate.push.../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
    at Object.onInvokeTask (core.js:26754)
    at ZoneDelegate.push.../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:422)
    at Zone.push.../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:195)
    at drainMicroTaskQueue (zone.js:601)
    at ZoneTask.push.../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (zone.js:502)
    at invokeTask (zone.js:1693)

原来是过滤器的问题。已过滤掉的响应中的 Observable 未从拦截器返回。因此,我删除了过滤器并将其添加到 switchMap 中,为不满足过滤条件的响应返回 of(event)。