Search code examples
angularngrx

Ngrx makes my dispatched object read only


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?


Solution

  • 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.