未捕获的TypeError:无法添加属性12,对象不可扩展 [英] Uncaught TypeError: Can't add property 12, object is not extensible

查看:219
本文介绍了未捕获的TypeError:无法添加属性12,对象不可扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法理解我在客户端应用程序上遇到的错误.我正在订阅graphql订阅,并且能够检索更新,但是无法将更改推送到绑定到视图的打字稿数组"models:ModelClass []".

I can't seem to understand the error I am getting on my client application. I am subscribing to a graphql subscription and I am able to retrieve the updates but I am not being able to push the changes to the typescript array called "models:ModelClass[]" which is bound to the view.

我有什么想念的或者做错了吗?

Is there something I am missing or doing 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);
});

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;
}

推荐答案

我想this.models是Apollo返回的数组,您想将新创建的对象添加到初始数组中吗?如果为true,则Apollo返回一个不可变的Object!

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

您必须克隆初始返回的数组.订阅功能中的内容:

You have to clone the initial returned array. Something like in the subscribe function:

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数组.

Then your subscription request will be able to add a created model to this plain javascript array.

PS:不确定,但我想Apollo返回不可变对象,因为它们存储在商店中,并且根据您的提取策略,如果您能够对其进行突变,它可能会错过商店匹配.

PS: Not sure but I suppose Apollo returns immutable objects because they are stored in the store and depending on your fetch policy, it can miss store hits if your are able to mutate them.

希望有帮助

这篇关于未捕获的TypeError:无法添加属性12,对象不可扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆