类型错误:您在需要流的地方提供了无效的对象。您可以提供 Observable、Promise、Array 或 Iterable

2024-05-19

我在尝试着map来自服务调用但收到错误。 看着subscribe 没有在 Angular 2 中定义吗? https://stackoverflow.com/questions/41995647/subscribe-is-not-defined-in-angular-2它说为了订阅我们需要从运营商内部返回。我也有退货声明。

这是我的代码:

checkLogin(): Observable<boolean> {
  return this.service
    .getData()
    .map(
      (response) => {
        this.data = response;
        this.checkservice = true;
        return true;
      },
      (error) => {
        // debugger;
        this.router.navigate(["newpage"]);
        console.log(error);
        return false;
      }
    )
    .catch((e) => {
      return e;
    });
}

错误日志:

类型错误:您在需要流的地方提供了无效的对象。您可以提供 Observable、Promise、Array 或 Iterable


就我而言,该错误仅在 e2e 测试期间发生。这是由throwError在我的 AuthenticationInterceptor 中。

我从错误的来源导入了它,因为我使用了 WebStorm 的导入功能。我正在使用 RxJS 6.2。

Wrong:

import { throwError } from 'rxjs/internal/observable/throwError';

Correct:

import { throwError } from 'rxjs';

这是拦截器的完整代码:

import { Injectable } from '@angular/core';
import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';

@Injectable()
export class AuthenticationInterceptor implements HttpInterceptor {

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const reqWithCredentials = req.clone({withCredentials: true});
    return next.handle(reqWithCredentials)
     .pipe(
        catchError(error => {
          if (error.status === 401 || error.status === 403) {
            // handle error
          }
          return throwError(error);
        })
     );
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

类型错误:您在需要流的地方提供了无效的对象。您可以提供 Observable、Promise、Array 或 Iterable 的相关文章

随机推荐