Uncaught TypeError: cannot add property 12, the object is not expanding

I cannot understand the error that I am getting in my client application. I subscribe to a graphql subscription and I can get updates, but I can't make changes to the typescript array called "models: ModelClass []", which is associated with the view.

Is there something that I am missing or something is wrong?

models.component.ts

this.apollo.subscribe({
  query: gql`
    subscription {
      newModelCreated{
        _id
        name
        type
        train_status
        deploy_status
        data_path
        description
        created_at
        updated_at
      }
    }
  `
}).subscribe((data) => {
  console.log("CREATED: " + JSON.stringify(data.newModelCreated));
  console.log(data.newModelCreated);
  var temp:ModelClass = data.newModelCreated;
  this.models.push(temp);
});

<strong> model-class.ts

export interface ModelClass {
    _id: string;
    name: string;
    type: string;
    parameters: {
        alpha: number;
    };
    train_status: string;
    deploy_status: string;
    test_accuracy: string;
    created_at: number;
    updated_at: number;
}
+4
source share
1 answer

I assume this.models is the array returned by Apollo and you want to add the new created object to your initial array? If true, Apollo returns an immutable Object!

. - :

this.apollo
    .watchQuery({query: INITIAL_GQL_REQUEST})
    .subscribe((data) => {
        this.models = data.models.map((model) => {
            return {
                id: model.id, 
                name: model.name,
                another: model.another
            }
        })
    };
});

javascript.

PS: , , Apollo , , .

,

0

Source: https://habr.com/ru/post/1675413/


All Articles