3
votes

I am using Angular NGRX for state management in my angular application. On dispatching login action i receives CognitoUser object from my login service as i am using AWS amplify service.

When i dispatch LoginSuccess action to pass this object i receives error TypeError: Cannot freeze. Login Success action is dispatched but data object is not received in reducer.

Here is my effect code

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
  ) {}
}

However if i assign this response user into another object like this

const obj = { ...user };

Then i am receiving data in reducer but in case of CognitoUser object i am not receiving data in reducer.

How can i pass CognitoUser object in reducer?

2
I have the same issue, also not using ngrx-store-freeze. Did you make any progress?Andresch Serj
@AndreschSerj my issue was that i was getting data of type CongnitoUser object from service. That's why i was not able to pass that data to reducer. But if you convet your data in normal json using JSON.parse(JSON.stringify(data)) before passing it to success action than you will be able to access it.Vinit Singh
If you can put that into an answer you can get teh 50 Point Bounty i just set on your question :-)Andresch Serj
Thanks @AndreschSerj. I posted answer with sample code.Vinit Singh

2 Answers

3
votes

My issue was that i was getting data of type CongnitoUser object from service. That's why i was not able to pass that data to reducer. But if you convet your data in normal json using JSON.parse(JSON.stringify(data)) before passing it to success action than you will be able to access it.

So effect code will be like this.

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)
  })
 )
);
-2
votes

It appears you are using the ngrx-store-freeze package. It causes the error.

There is an open related issue here: https://github.com/brandonroberts/ngrx-store-freeze/issues/17

This package is clearly not maintained so I think you should remove it in your project.