7

In the constructor I do something like this

selectedDate: Object;
// construtor
this.selectedDate = {};
this.selectedDate['date'] = new Date();
this.selectedDate['pristine'] = new Date();

In another function which gets called on a button click I do the following:

this.selectedDate['date'] = new Date(this.selectedDate['pristine']);

And I get the following error:

TypeError: Cannot assign to read only property 'date' of object '[object Object]'

4
  • can you show the onClick function Commented Jun 27, 2017 at 16:47
  • 4
    Are you using redux? If this.selectedDate is somehow put in an action payload and assigned to the state in a reducer you won't be able to modify it directly without another action going through the pipeline. Commented Jun 27, 2017 at 16:47
  • Sounds like you should just define the type rather than using Object; at that point you might as well use any and be done with it. IOW, use selectedDate: { date: Date, pristine: Date } (or whatever your properties are). Commented Jun 27, 2017 at 16:50
  • @Ryan: Yes, I am using redux and now I understand why I am seeing this issue. Your analysis is correct, thanks !! Commented Jun 27, 2017 at 16:52

2 Answers 2

6

Credits to Ryan Q:

As Ryan commented I was using ngRx in my app and I was indeed passing this.selectedDate object through an action into the store and hence I was not able to modify the object that is stored in the State.

One way to resolve this is pass a new object reference and this should resolve the issue:

this.store.dispatch(new settime.DateChangeAction(Object.assign({}, this.selectedDate)));
Sign up to request clarification or add additional context in comments.

Comments

2

@vikas' answer put me on the right track to solve a similar issue.

I'm trying to handle an upload, where I want to display the progress:
- From the UploadComponent, I emit an event to the parent
- The parent dispatch an action to the store
- An effect catches the action, call the service to upload the file (which returns an array with two observables: progress$ and result$)
- The effect dispatch an HttpProgress action which contains the observable

And at this point I had a similar issue. After digging into store freeze code, a coworker and I found out that it also deep freeze the action.payload. And thus, passing an observable results into an error as observable state is mutated internally.

I'm not using the payload to keep it into the store, just so I can register to actions$ within my component so here it doesn't matter if I pass a mutable object into the payload. To avoid that error, I just wrapped the observable into a function as deep freeze do not freeze functions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.