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

查看:26
本文介绍了未捕获的类型错误:无法添加属性 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 返回一个不可变对象!

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.

希望能帮到你

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

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