frappe / gantt

Open Source Javascript Gantt
https://frappe.io/gantt
MIT License
4.67k stars 1.04k forks source link

fix: Cannot read properties of undefined (reading '$bar') #392

Closed iPopstop closed 6 months ago

iPopstop commented 6 months ago

I think the problem is in the filter in the get_bars function

https://github.com/frappe/gantt/blob/ea6259adceff6e7032d0abd4dcf87ff278251bee/src/index.js#L973C3-L977C4

get_bar(id) {
    return this.bars.find((bar) => {
      return bar.task.id === id;
    });
 }

Type of id can be String and type of bar.task.id is Number. We can:

Also i added filter to this line https://github.com/frappe/gantt/blob/ea6259adceff6e7032d0abd4dcf87ff278251bee/src/index.js#L782

bars = ids.map((id) => this.get_bar(id)).filter(bar => bar !== undefined);

With this filter we will not get an error if result of get_bar(id) is undefined

Same with get_task(task_id) (dependency arrows doesn't show because result of get_task is undefined): Dependency also has a strict comparison and if id is a number then the passed task_id could be a string (I use a numeric id to work with events from the database) and task.id could be a number

safwansamsudeen commented 6 months ago

Thanks for opening a PR and catching the bug.

However, I don't think this is the correct way to go about it. In #393, I've enforced string based IDs (though users can pass in integers that will be converted).