Skip to content
Advertisement

vanilla js error “cannot set property of undefined” on the chrome console

Hey I’m new to chartjs and js in general and keep getting the error “”cannot set property of undefined” on the chrome console

JavaScript

then in a timed loop I’m calling this and getting the error “cannot set property of undefined” when trying to do the below operation (I’m not setting the value in html and it is only set when building the pie-chart at the top).

JavaScript

like I said I’m very new to js so if anyone can help that would be great!

Advertisement

Answer

It looks like you were accessing the data wrong, I will include a link to the documentation. I have modified your snippet below (I don’t have a timer, but I have the code changing). Make sure you call update after changing the value so it will reflect in the HTML.

datasets is an array, so you need to access the index of the dataset you wish to modify. In your case, it is [0] as you only have one dataset.

ChartJs Documentation: https://www.chartjs.org/docs/latest/developers/api.html

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement