类型错误:您提供了一个预期流的无效对象.你可以提供一个 Observable、Promise、Array 或 Iterable [英] TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable

查看:17
本文介绍了类型错误:您提供了一个预期流的无效对象.你可以提供一个 Observable、Promise、Array 或 Iterable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从服务调用中map,但出现错误.查看 订阅未在 angular 2 中定义? 并说为了订阅,我们需要从运营商内部返回.我也有 return 语句.

I am trying to map from a service call but getting an error. Looked at subscribe is not defined in angular 2? and it said that in order to subscribe we need to return from inside the operators. I have return statements as well.

这是我的代码:

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

错误日志:

TypeError:您提供了一个无效的对象,其中需要一个流.你可以提供一个 Observable、Promise、Array 或 Iterable

TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable

推荐答案

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

In my case the error occurred only during e2e tests. It was caused by throwError in my AuthenticationInterceptor.

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

I imported it from a wrong source because I used WebStorm's import feature. I am using RxJS 6.2.

错误:

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

正确:

import { throwError } from 'rxjs';

这里是拦截器的完整代码:

Here the full code of the interceptor:

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

这篇关于类型错误:您提供了一个预期流的无效对象.你可以提供一个 Observable、Promise、Array 或 Iterable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆