Can't call method of an object that is part of the observable array #2934
-
Hi! (...)
service: IServices;
myTasks: Task[] = [];
favBusinesses: IBusiness[] = [];
constructor (service: IServices)
{
makeAutoObservable(this, { service: false });
this.service = service;
}
async updateMyTask (newTask: TaskCrud): Promise<void>
{
// Void service method that updates the task on the server
//await this.service.taskService.updateTask(newTask);
runInAction(() => {
this.myTasks.forEach( (item, idx) => {
if(item.id === newTask.id)
{
item.changeData({ description: "Some description" } as ITask); // <===== Error
}
});
// If i update it like this then it works!
// x.highPrice = 100000;
//x.description = "This works";
});
console.log(JSON.stringify(this.myTasks.find(x => x.id === newTask.id))) // --> It's not null!
}
changeData (data: ITask): void
{
this.description = data.description ?? this.description;
this.lowPrice = data.lowPrice ?? this.lowPrice;
this.highPrice = data.highPrice ?? this.highPrice;
this.username = data.username ?? this.username;
this.email = data.email ?? this.email;
}
export interface TaskCrud
{
description?: string;
highPrice?: number;
lowPrice?: number;
id?: string;
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
I can't spot an obvious problem with this, so for any help a reproduction is needed. The most likely problem is that you have some plain json objects in your |
Beta Was this translation helpful? Give feedback.
I can't spot an obvious problem with this, so for any help a reproduction is needed. The most likely problem is that you have some plain json objects in your
myTasks
collection, that are not actualTask
class instances, so they will have theid
etc fields, but not achangeData
method. I'd check any code that fills the collection.