Reputation: 1895
I am trying to figure out how @ngrx/effects works, but I am having some problems in this easy example below my error and my stackbliz code
https://stackblitz.com/edit/redux-in-actions?file=src%2Fapp%2Fstore%2Feffects%2Ftask.effects.ts
Type 'Observable' is not assignable to type 'Observable'. Type 'void' is not assignable to type 'Action'
thank
Upvotes: 6
Views: 9446
Reputation: 9764
In RXJS, inside map operator, you need to return values will be received when observer subscribes.
@Effect()
loadTasks$: Observable<Task[]> = this.actions$
.ofType(fromActions.ADD_TASK).pipe(
switchMap(() => ({this.taskService.GetTasks().map(data => {
return data; })
}))
);
Upvotes: 5
Reputation: 311
There are a couple of things wrong just to start with
pipe(map(data => ...)
from import { map } from 'rxjs/operators'
.dispatch: false
since you are not returning any actions, otherwise you will get stuck in an endless loop.An effect should either return an action or dispatch: false
@Effect({ dispatch: false })
loadTasks$: Observable<void> = this.actions$.pipe(
ofType(fromActions.ADD_TASK),
switchMap(() => this.taskService.GetTasks().pipe(
map(data => {
console.log(data);
})
)
));
@Effect()
loadTasks$: Observable<void> = this.actions$.pipe(
ofType(fromActions.ADD_TASK),
switchMap(() =>
this.taskService.GetTasks().pipe(
map(
(data: Task[]) => new SomeActionYouGot(data);
),
catchError(error =>
of(new SomeOtherActionThatIndicatesFailFor(data))
)
)
);
Upvotes: 2