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

查看:61
本文介绍了TypeError:您提供了一个无效的对象,该对象应在其中流.您可以提供一个Observable,Promise,Array或Iterable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从服务呼叫中退出map,但出现错误. 看着 subscribe在角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: 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 'rjxs/internal/observable/throwError';

正确:

import { throwError } from 'rjxs';

这是拦截器的完整代码:

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

这篇关于TypeError:您提供了一个无效的对象,该对象应在其中流.您可以提供一个Observable,Promise,Array或Iterable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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