@Effect() /* sends request to httpService with params as login credentials on instance of loginAction. */
login$: Observable<Action> = this.actions$
.instanceOf(LoginActions.LoginAction)
.switchMap(
action => {
return this.loginHttpService.login(action.payload)
.map( (res: any) => {
if (res && res.message !== 'Invalid Login') {
const firstName = res.firstName;
const lastName = res.lastName;
this.tokenService.setToken(res.jwt);
this.tokenService.setFirstName(firstName.charAt(0).toUpperCase() + firstName.slice(1));
this.tokenService.setLastName(lastName.charAt(0).toUpperCase() + lastName .slice(1));
this.tokenService.setId(res.id);
this.tokenService.setAvatar(firstName.charAt(0).toUpperCase() + lastName.charAt(0).toUpperCase());
const perm = ['ADMIN']
this.tokenService.setUserRoles(perm)
return Observable.create(observer => {
this.permissionsService.loadPermissions(perm, () => {
observer.next({
type: 'string'
});
return observer.complete();
})
})
}
}).catch( (e:any) => {
return Observable.of(new LoginActions.LoginFailureAction(true));
});
});
我正在尝试从loadPermissions
内部返回。但是我得到一个错误:
Effect "LoginEffects.login$" dispatched an invalid action
答案 0 :(得分:1)
如果您尝试退出效果而不分派动作,则需要将disaptach
设置为false
。您可以用@Effect({ dispatch: false })
装饰效果,然后在所有返回操作的地方都需要自己调用this.store.dispatch(/* Some Action */);
。
@Effect({ dispatch: false })
login$: Observable<Action> = this.actions$
.instanceOf(LoginActions.LoginAction)
.switchMap(
action => {
this.loginHttpService.login(action.payload)
.map( (res: any) => {
if (res && res.message !== 'Invalid Login') {
// Logic omitted
this.permissionsService.loadPermissions(perm, () => {
// Dispatch an action
this.store.dispatch(new LoginActions.LoginSuccessAction(true)));
});
}
}).catch( (e:any) => {
this.store.dispatch(new LoginActions.LoginFailureAction(true)));
});
});
constructor(private store: Store<any>) {}