javascript - Error: El efecto despachó una acción no válida: indefinido
Estoy intentando escribir un efecto para mi acción. Tiene 2 métodos de servicio: getData() y checkToken() dentro.
Generará un error:
"ERROR Error: Effecto \"IndexEffects.loginStatusValidate$\" despachó una acción inválida: undefined"
¿Qué debo hacer?
@Effect()
loginStatusValidate$: Observable<Action> = this.actions$.pipe(
ofType(IndexActionTypes.LoginStatusValidate),
switchMap(() =>
this.authService.getData().pipe(
map(oldToken => {
if (oldToken) {
console.log("oldToken exsits");
this.authService.checkToken().subscribe((newToken) => {
console.log(newToken);
// Throw error if I try to dispatch action here
return new LoginStatusValidateSuccess(newToken);
});
// It's fine if I just dispatch action here
//return new LoginStatusValidateSuccess('');
} else {
return new LoginStatusValidateError();
}
})
)
)
);
getData() {
return of(localStorage.getItem('ACCESS_TOKEN'));
}
checkToken(){
// access api refresh token
return of("newToken");
}
Respuesta #1
El problema es this.authService.checkToken().subscribe((newToken) => {, al hacer esto no estás devolviendo algo, estás devolviendo undefined.
Estás suscribiéndote, en su lugar debes devolver un flujo de acciones, como lo haces con this.authService.getData().pipe.