0

I've got the following lines of code:

this.carInformation.subModel = [this.submodel.value]; 
this.store.dispatch(saveCriteria({ criteria: this.carInformation }));

When my app re-runs these two lines, I get the following error on the first line:

Cannot assign to read only property 'subModel' of object '[object Object]'

Why is carInformation suddenly read only?

I did notice using the following line of code that the writable property of my object is now false:

Object.getOwnPropertyDescriptor(this.carInformation, 'subModel');

I managed to fix this by moving the dispatch elsewhere, but I would've figured that I could re-edit my object and re-dispatch it?

Drayen
  • 3
  • 1
  • Does this answer your question? [Angular ngrx: assign a readonly property](https://stackoverflow.com/questions/61404762/angular-ngrx-assign-a-readonly-property) – David Fontes Mar 23 '22 at 19:23
  • Additionally, here are a couple more of resources that can help you. [Link 1](https://stackoverflow.com/questions/57591012/ngrx-cannot-assign-to-read-only-property-property-of-object-object) [Link 2](https://stackoverflow.com/a/62066775/11755228). I recommend reading the "Link 2" as it is golden IMHO. – David Fontes Mar 23 '22 at 19:46

1 Answers1

0

The reference of this.submodel.value is dispatched to the store. Because the store has immutable runtime checks, that reference is "locked" and throws if it's changed.

timdeschryver
  • 14,415
  • 1
  • 19
  • 32