Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot add property "X", object is not extensible angular 9

I have encountered this issue on many places after some days of updating from Angular 8 to Angular 9. Earlier it was working fine.

Can anyone help me what's the problem. Example code below

list.reduce((acc, curr) => {
        const obj = {};
        obj['x'] = curr['Y'];
});
like image 762
Ashish S Avatar asked Mar 11 '20 07:03

Ashish S


People also ask

How to fix object is not extensible?

To fix this error, you will either need to remove the call to Object. preventExtensions() entirely, or move it to a position so that the property is added earlier and only later the object is marked as non-extensible. Of course you can also remove the property that was attempted to be added, if you don't need it.

How do I remove preventExtensions?

There is no way to make an object extensible again once it has been made non-extensible. It is important to note that Object. preventExtensions only prevents the extension of the top level of the object. In the example above, person.


2 Answers

If you are getting this error after using NgRx in Angular. Try implementing below code it will help on a global level. Otherwise you can utilize deep cloning of Object or Array to eradicate the problem.

@NgModule({
  imports: [
  StoreModule.forRoot(reducers, {
    runtimeChecks: {
      strictStateImmutability: false,
      strictActionImmutability: false,
    },
  }),
 ],
})
export class AppModule {}
like image 107
Manas Avatar answered Sep 25 '22 23:09

Manas


I refer change list of Ngrx version 8 to 9 and migration guideline here

https://ngrx.io/guide/migration/v9

As I found there is a special change related to immutability with angular 9. They have defined Action, state and serializability related immutability logic there. And I tried out the method that they have suggested to resolve those issues with Ngrx V9 update here

https://ngrx.io/guide/store/configuration/runtime-checks

@ngrx/store ships with five (5) built-in runtime checks. Try disabled all checks

like image 21
Jane Kulakovski Avatar answered Sep 24 '22 23:09

Jane Kulakovski