Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Cannot add property 1, object is not extensible↵ at Array.push (<anonymous>)

I am trying to add some data to an array but I am getting not extensible error.

Component code:

this._store.select(p => p.collection.workspaceCollectionPages).subscribe((data: CollectionPageDbModel[]) =>{
      if(data){
      return data.map((collectionPageDbModel)=> {

      this.collectionPages.push(collectionPageDbModel) //Getting error on this line while pushing

    });}

enter image description here

I have four objects in my data var, which I am trying to push in collectionPages, but I am getting extensible error while pushing.

enter image description here collectionPage array where i need to add more data

enter image description here This data I need to push

CollectionPageDbModel:

export class CollectionPageDbModel implements IDbModel {
  public id?: number;
  public collection_version_id: number;
  public user_id?: string;
  public name: string;
  public position?: number = 0;
  public contents: string;
  public created_at?: string;
  public updated_at?: string;
  public server_id?: any;

}

Can someone please help me out resolving this

like image 742
CoderBeginner Avatar asked Sep 25 '18 06:09

CoderBeginner


People also ask

Can not add property 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.

Is Javascript arrays are extensible?

Built-in classes like Array, Map and others are extendable also.


1 Answers

Make a copy of object using the Object.assign method and try again,

this.collectionPages = Object.assign([], this.collectionPages);
this.collectionPages.push(collectionPageDbModel);
like image 158
Daniel Inbaraj Avatar answered Sep 24 '22 02:09

Daniel Inbaraj