Reputation: 21
I am trying to write an effect for my action. It has 2 service methods: getData()
and checkToken()
inside.
It will throw an error:
"ERROR Error: Effect "IndexEffects.loginStatusValidate$" dispatched an invalid action: undefined"
What should I do ?
@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");
}
Upvotes: 2
Views: 1644
Reputation: 15505
The problem is this.authService.checkToken().subscribe((newToken) => {
, by doing this you're not returning something, you're returning undefined
.
You're subscribe, instead you must return a stream of actions, like you do with this.authService.getData().pipe
.
Upvotes: 1