[英]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.