简体   繁体   English

未捕获的TypeError:无法添加属性12,对象不可扩展

[英]Uncaught TypeError: Can't add property 12, object is not extensible

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. 我正在订阅graphql订阅,我能够检索更新,但我无法将更改推送到名为“models:ModelClass []”的视频脚本数组,该数组绑定到视图。

Is there something I am missing or doing wrong? 有什么我遗失或做错了吗?

models.component.ts 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 模型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;
}

I suppose this.models is an array returned by Apollo and you want to add new created object to your initial array ? 我想this.models是Apollo返回的数组,你想在你的初始数组中添加新创建的对象吗? If true, Apollo returns an immutable Object ! 如果为true,Apollo将返回一个不可变的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
            }
        })
    };
});

Then your subscription request will be able to add a created model to this plain javascript array. 然后您的订阅请求将能够将创建的模型添加到此普通javascript数组。

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. PS:不确定,但我认为Apollo会返回不可变对象,因为它们存储在商店中,并且根据您的获取策略,如果您能够改变它们,它可能会错过商店命中。

Hope it helps 希望能帮助到你

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的类型错误:无法定义属性“x”:对象不可扩展 - Uncaught TypeError: can't define property "x": Object is not extensible React Error(TypeError):无法添加属性上下文,对象不可扩展 - React Error (TypeError): Can't add property context, object is not extensible 未捕获的类型错误:无法添加属性 0,object 在 Array.push 不可扩展 - Uncaught TypeError: Cannot add property 0, object is not extensible at Array.push 无法添加属性_tracking,对象不可扩展 - Can't add property _tracking, object is not extensible 类型错误:无法定义属性“当前”:Object 不可扩展 - TypeError: can't define property “current”: Object is not extensible TypeError:无法添加属性 0,object 不可扩展 - TypeError: Cannot add property 0, object is not extensible 为什么我在 redux-slice 中收到错误消息“Uncaught (in promise) TypeError: Cannot add property bookId, object is not extensible”? - Why I am getting an error as "Uncaught (in promise) TypeError: Cannot add property bookId, object is not extensible" into my redux-slice? React App-TypeError:无法添加属性setState,对象不可扩展 - React App - TypeError: Cannot add property setState, object is not extensible 材料表类型错误:无法添加属性表数据,对象不可扩展 - Material-table TypeError: Cannot add property tableData, object is not extensible Jest TestEnvironment - 类型错误:接下来无法添加属性,object 不可扩展 - Jest TestEnvironment - TypeError: Cannot add property next, object is not extensible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM