I use Angular 7 NGRX state system.
When I dispatch this method from my component, I get error.
Parameters are correct. I checked them all. I think I made mistake by using "mergeMap" and "return" wrong value.
Where am I mistaken at this code ?
Error :
ERROR Error: Effect "StudentEffects.addReview$" dispatched an invalid action: [object Object] .....
ERROR TypeError: Actions must have a type property
Action Methods :
export class AddReview implements Action {
readonly type = ActionTypes.AddReview;
constructor(public payload: { guruId: string, review: IReview }) { }
}
export class AddReviewSuccess implements Action {
readonly type = ActionTypes.AddReviewSuccess;
constructor(public payload: { guruId: string, review: IReview }) { }
}
Effect Method :
@Effect()
addReview$ = this.actions$.pipe(
ofType<StudentActions.AddReview>(StudentActions.ActionTypes.AddReview),
mergeMap(action => {
return this.personApi.findOne({ where: { id: action.payload.guruId } }).pipe(
map((guru: Person) => {
let reviews: Array<IReview> = Array.isArray(guru._reviews) ? guru._reviews : [];
reviews.push(action.payload.review);
let attrs = {
_reviews: reviews,
};
return this.personApi // => Not goes inside. I get error.
.patchAttributes(guru.id, attrs)
.pipe(
map(person => {
this.notificationService.createNotification(
'success', 'Success', 'Your review successfully saved.'
);
this.logService.groupLog('AddReview', action, person);
return new StudentActions.AddReviewSuccess({ guruId: guru.id, review: action.payload.review });
}),
catchError(() => {
// this.notificationService.createNotification(
// 'error',
// 'Error',
// 'There is an error on save'
// );
console.error('There was an error');
return EMPTY;
})
);
}),
catchError(() => EMPTY)
);
})
);
Reducer Methods :
case ActionTypes.AddReview: {
return {
...state,
loading: true
};
}
case ActionTypes.AddReviewSuccess: {
return {
...state,
reviews: [
...state.reviews,
{ guruId: action.payload.guruId, review: action.payload.review }
],
loading: false
};
}
EMPTY
? – Yevgeniy.Chernobrivetsreturn
andmap
. EMPTY doesn't even work. – canmustuEMTY
is not actually an action. – Yevgeniy.Chernobrivets