重置-Redux抛出调度了无效的操作:未定义

时间:2019-04-01 20:40:18

标签: redux ngrx ngrx-store ngrx-effects ngrx-entity

我正在尝试基于注销重置我的redux-store,但是出现以下错误。寻找更好的建议来重置我的redux-store状态,以便在我发现将其设置为undefined的任何地方都可以解决问题,但在我的情况下,它不起作用。

    /**
 * Our state is composed of a map of action reducer functions.
 * Here we are going to reset the state of root reducer to undefined to clear data store
 * Logged Action is triggered from User session effects.
 */
export function resetState(reducer: ActionReducer<State>): ActionReducer<State> {
  return function (state: State, action: UserSessionActions.UserSessionActionsUnion): State {

    switch (action.type) {
      case UserSessionActions.UserSessionActionTypes.LoggedOut: {
        state = undefined;
        return reducer(state, action);
      }
      default: {
        return reducer(state, action);
      }
    }
  };
}

但遇到此异常

ERROR Error: Effect "UserSessionEffects.logout$" dispatched an invalid action: [object Object]
at reportInvalidActions (effects.es5.js:236)
at verifyOutput (effects.es5.js:214)
at MapSubscriber.project (effects.es5.js:284)
at MapSubscriber._next (map.js:79)
at MapSubscriber.Subscriber.next (Subscriber.js:90)
at SwitchFirstMapSubscriber.notifyNext (exhaustMap.js:113)
at InnerSubscriber._next (InnerSubscriber.js:23)
at InnerSubscriber.Subscriber.next (Subscriber.js:90)
at MapSubscriber._next (map.js:85)
at MapSubscriber.Subscriber.next (Subscriber.js:90)

这是我的作用

    @Effect()
  logout$ = this.actions$.pipe(
    ofType(UserSessionActions.UserSessionActionTypes.LoggedOut),
    map((action: any) => {
      this.logger.log('logout ' + JSON.stringify(action));
      return Observable.of({});
    }),
    catchError((err) => {
      this.logger.log(err);
      return Observable.of();
    })
  );

1 个答案:

答案 0 :(得分:1)

  

错误错误:效果“ UserSessionEffects.logout $”调度了无效的操作

除非将其装饰为@Effect({dispatch: false}),否则ngrx / effect必须返回一个动作(具有type属性的对象)。

更新效果以返回操作

return Observable.of({ type: 'LOGGED OUT SUCCESS'});