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

前端 未结 20 1105
时光说笑
时光说笑 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:33

    I had the same issue caused by importing the internal version of 'takeUntil' instead of the operators Change

    import { takeUntil } from 'rxjs/internal/operators/takeUntil';
    

    to

    import { takeUntil } from 'rxjs/operators';
    

    This happen also for other operators

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

    In my case in Angular-5, service file was not imported from which i was accessing the method and subscribing the data.After importing service file it worked fine.

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

    I was also facing the same issue when i was calling a method inside switchMap, apparently I found that if we use method inside switchMap it must return observable.

    i used pipe to return observable and map to perform operations inside pipe for an api call which i was doing inside method rather than subscribing to it.

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

    In my case I mistakely imported Action into my combineEpics, rather than Epic...

    Verify all the functions within combine Epics are epic funcitons

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

    In regard to the "You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable" error.

    This could happen if you import { Observable } from 'rxjs' after (below) some module/function/whatever, which actually uses it.

    Solution: move this import above the import of that module.

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

    I have the same exact error message while I was doing my unit test and throwing observable exception after mocking my services.

    I resolved it by passing exact function and format inside Observable.throw.

    Actual code which calls the service and subscribe to get data. notice that catch to handle the 400 error.

         this.search(event).catch((e: Response) => {
            if (e.status === 400) {
              console.log(e.json().message);
            } else if (e.url) {
              console.log('HTTP Error: ' + e.status + ' ' + e.statusText,
                'URL: ' + e.url, 'Info: ' + e.json().message));
            }
          }).finally(() => {
            this.loading = false;
          }).subscribe((bData) => {
            this.data = bData;
          });
    

    The code inside the service

      search() {
        return this.someService.getData(request)
           .do((r) => {
              this.someService.defaultHeaders.delete('skipAlert');
              return r;
            })
          .map((r) => {
              return r.businessObjectDataElements.length && r.businessObjectDataElements || null;
            });
      }
    

    Unit Testing

    I have mocked the SomeService and returning observable data and its fine as it have all the required methods inside it.

     someServiceApi = fixture.debugElement.injector.get(SomeService);
     spyOn(someServiceApi, 'getData').and.returnValue(Observable.of({}));
    

    The above code is okey but when when I was trying to test the catch/error condition by passing Observable.throw({}) it was showing me the error as it was expecting Response type return from the service.

    So below service mocking return was giving me that error.

    someServiceApi.getData
      .and.returnValue(Observable.throw(new Response({status: 400, body: [], message: 'not found error'})));
    

    So I Corrected it by replicating the exact expected function in my return object rather passing a Response type value.

    someServiceApi.getData
      .and.returnValue(Observable.throw({status: 400, json: () => { return {message: 'not found error'}}, body: []}));
    // see `json: () => { return {message: 'not found error'}}` inside return value
    
    0 讨论(0)
提交回复
热议问题