I am trying to write an effect for an action but it is throwing an error as: “Effect “n.loadInfo$” dispatched an invalid action: null Error“
I have my effect as below:
JavaScript
x
20
20
1
@Effect()
2
loadInfo$ =
3
this.actions.ofType(fromHeaderActions.EInfoActions.OPEN_INFO).pipe(
4
withLatestFrom(
5
this.store.select(fromSelectors.GetINFOPayload)),
6
switchMap(([action, infoPayLoad]) => {
7
let cAction: fromHeaderActions.OpenINFOWIdget =
8
<fromHeaderActions.OpenINFOWIdget>action;
9
return this.infoService.loadINFO(infoPayLoad).pipe(
10
// Dispatch success action
11
map(response => new
12
fromHeaderActions.OpenINFOWIdgetSuccess(response)),
13
catchError(error => {
14
return of(new
15
fromHeaderActions.OpenINFOWIdgetFail(error.message))
16
})
17
)
18
})
19
);
20
I have my action defined as below:
JavaScript
1
24
24
1
export class OpenINFOWIdget extends BaseGetDetailsAction implements Action {
2
readonly type = EInfoActions.OPEN_INFO;
3
constructor() {
4
super();
5
}
6
}
7
8
9
//added action after the comment
10
export class OpenINFOWIdgetSuccess extends BaseGetDetailsAction
11
implements Action {
12
readonly type = EInfoHeaderActions.OPEN_INFO_SUCCESS;
13
constructor(public payload: INFO) {
14
super();
15
}
16
}
17
18
export class OpenINFOWIdget extends BaseGetDetailsAction implements Action {
19
readonly type = EInfoActions.OPEN_INFO_FAIL;
20
constructor(public payload: string) {
21
super();
22
}
23
}
24
And in the service as below:
JavaScript
1
14
14
1
public INFOPayloadsource = new BehaviorSubject<INFO>(initINFO);
2
infoPayload$ = this.INFOPayloadsource.asObservable();
3
4
private SelectedInfoSource = new Subject<INFO>();
5
selectedInfo$ = this.SelectedInfoSource.asObservable();
6
7
loadINFO(payload: INFO): Observable<INFO> {
8
if (payload != null) {
9
this.IsInfoEnableSource.next(true);
10
this.InfoPayloadsource.next(payload);
11
}
12
return this.selectedInfo$;
13
}
14
I have selector as below that is used in the effect:
JavaScript
1
16
16
1
export const GetINFOPayload = createSelector(getInfoState,
2
(state:
3
InfoDetailsState) => {
4
if (state) {
5
if (state.infoDetails != null &&
6
state.infoDetails.INFODetail !=
7
null &&
8
state.infoDetails.INFODetail.Active != null) {
9
let payload: INFO = { initINFO };
10
payload = state.infoDetails.INFODetail.Active;
11
return payload;
12
}
13
}
14
return null;
15
});
16
Create reducer as below after the comment:
JavaScript
1
16
16
1
case
2
fromInfoDetailsHeaderActions.EInfoHeaderActions.OPEN_INFO: {
3
return {
4
state,
5
IsScreenOverlay: true,
6
IsEditable: false
7
}
8
};
9
case fromInfoDetailsHeaderActions.EInfoHeaderActions.OPEN_INFO_SUCCESS: {
10
return {
11
state,
12
IsScreenOverlay: false,
13
IsEditable: true
14
}
15
};
16
I would really appreciate if anyone can help on it. Thank you!
Advertisement
Answer
Your effect is trying to dispatch the result of this.infoService.loadINFO()
, which is why you get the error saying that it is not a valid action.
You should map
this to a success action instead:
JavaScript
1
18
18
1
@Effect()
2
loadInfo$ = this.actions.ofType(fromHeaderActions.EInfoActions.OPEN_INFO).pipe(
3
withLatestFrom(
4
this.store.select(fromSelectors.GetINFOPayload)
5
),
6
switchMap(([action, infoPayLoad]) => {
7
let cAction: fromHeaderActions.OpenINFOWIdget = <fromHeaderActions.OpenINFOWIdget>action;
8
9
return this.infoService.loadINFO(infoPayLoad).pipe(
10
// Dispatch success action
11
map(response => new fromHeaderActions.OpenINFOWIdgetSuccess(response)),
12
catchError(error => {
13
return of(new fromHeaderActions.OpenINFOWIdgetFail(error.message))
14
})
15
)
16
})
17
);
18
You will also need to add the corresponding action and handle it in your reducer, if necessary.