当从effect中调度action时,Angular NGRX无法将数据传递给reducer。出现错误 - TypeError:无法冻结

Nin*_*tle 5 javascript rxjs ngrx angular aws-amplify

我在我的角度应用程序中使用 Angular NGRX 进行状态管理。在调度登录操作时,我CognitoUser从我正在使用的登录服务接收对象AWS amplify service

当我调度 LoginSuccess 操作来传递此对象时,我收到错误TypeError: Cannot freeze。登录成功操作已调度,但减速器中未收到数据对象。

这是我的效果代码

export class LoginEffects {
  login$ = createEffect(() =>
    this.actions$.pipe(
      ofType(LoginActions.login),
      switchMap((action) =>
        from(this.authService.login(action.username, action.password))
      ),
      catchError((error) => of(LoginActions.loginError(error))),
      map((user) => LoginActions.loginSuccess(user))
   )
  );

  // ... other effects
  constructor(
    private actions$: Actions,
    private authService: AuthService,
    private router: Router
  ) {}
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我将此响应用户分配给另一个像这样的对象

const obj = { ...user };
Run Code Online (Sandbox Code Playgroud)

然后我在减速器中接收数据,但对于CognitoUser对象,我没有在减速器中接收数据。

我如何CognitoUser在减速器中传递对象?

Nin*_*tle 5

我的问题是我正在CongnitoUser从服务获取对象类型的数据。这就是为什么我无法将该数据传递给减速器的原因。但是,如果您在将数据传递给成功操作之前使用普通 json 转换数据JSON.parse(JSON.stringify(data)),那么您将能够访问它。

那么效果代码就会是这样的。

login$ = createEffect(() =>
this.actions$.pipe(
  ofType(LoginActions.login),
  switchMap((action) =>
    from(this.authService.login(action.username, action.password))
  ),
  catchError((error) => of(LoginActions.loginError(error))),
  map((user) => {
     user = JSON.parse(JSON.stringify(user));
     LoginActions.loginSuccess(user)
  })
 )
);
Run Code Online (Sandbox Code Playgroud)