Chartjs-ERROR TypeError: Cannot read property 'testMethod' of undefined

2👍

Meaning of this keyword in a Javascript function depends on the usage. See here for more details.

Either way it will not refer to the scope of the class. To refer to the scope of the class using the this keyword, use arrow function expression. Try the following

plugins: {  
    .
    .
    .
    onRefresh: (Linecap) => {
      console.log(this.testMethod())
      console.log(this.dataArray)
      Linecap.data.datasets[0].data.push({x: Date.now(), y: this.dataArray[2] });
      Linecap.update({ preservation: true });
    }
  }
}

1👍

The scope inside the onRefresh function is different than the component scope so this is undefined
use instead an arrow function for the onRefresh like this

onRefresh: (Linecap)  => { ... }

Leave a comment