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

前端 未结 20 1107
时光说笑
时光说笑 2020-12-01 06:22

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

相关标签:
20条回答
  • 2020-12-01 06:43

    You will get the following error message too when you provide undefined or so to an operator which expects an Observable, eg. takeUntil.

    TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable 
    
    0 讨论(0)
  • 2020-12-01 06:44

    I wrote this because I arrive here searching for the same error, and this could be useful for someone in the future.

    I get the same error while trying to initialize a service variable from its constructor making a call to a remote API trough http.get and .subscribe()

    After many tests without understanding what the problem was, i finally get it: My application had authentication and an HttpInterceptor, and i was trying to initialize the service calling a public API method using http.get(...) without 'No-Auth' headers. I added them like here, and problem solved for me:

    getData() {
    var reqHeader = new HttpHeaders({ 'Content-Type': 'application/x-www-urlencoded','No-Auth':'True' });    
    return this.http.get(environment.urlApi.Literales, { headers: reqHeader });  
    }
    

    What a headache :(

    0 讨论(0)
  • 2020-12-01 06:48

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

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

    Wrong:

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

    Correct:

    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);
            })
         );
      }
    }
    
    0 讨论(0)
  • 2020-12-01 06:48

    I was forgetting to return the other observable in pipe(switchMap(

    this.dataService.getPerson(personId).pipe(
      switchMap(person => {
         //this.dataService.getCompany(person.companyId); // return missing
         return this.dataService.getCompany(person.companyId);
      })
    )
    
    0 讨论(0)
  • 2020-12-01 06:48

    I'm not sure if this will help anyone, but in my case further up my chain I was using distinctUntilChanged and an exception inside a function there was manifesting with this error message.

    0 讨论(0)
  • 2020-12-01 06:49

    A hint for anyone experiencing this. This can happen when a switchMap doesn't receive an observable return value (like null). Simply add a default case, so it always returns an observable.

            switchMap((dateRange) => {
              if (dateRange === 'Last 24 hours') {
                return $observable1;
              }
              if (dateRange === 'Last 7 Days') {
                return $observable2;
              }
              if (dateRange === 'Last 30 Days') {
                return $observable3;
              }
              // This line will work for default cases
              return $observableElse;
            })
    
    0 讨论(0)
提交回复
热议问题