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
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
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.
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.
In my case I mistakely imported Action into my combineEpics, rather than Epic...
Verify all the functions within combine Epics are epic funcitons
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.
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;
});
}
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