V
V
Vladislav Dudashvili2021-05-15 18:57:44
Vue.js
Vladislav Dudashvili, 2021-05-15 18:57:44

Error "Cannot read property 'getContext' of undefined". What could it be?

I have a task to display a graph on the site, there is such a code:

watch: {
  dataset: {
    handler(newValue, oldValue) {
            if (newValue && newValue.length) {
      
      console.log(this.immediate);
      let chartData = {
        labels: [],
        datasets: [
          {
            label: "Data 1",
            backgroundColor: 'red',
            /* showLine:true, */
            data: this.dataset,
          },
        ],
      };
      this.dataset.forEach((item) => {
        chartData.labels.push(item.x);
        chartData.datasets[0].data.push(item.y);
      });
      this.renderChart(chartData, {
        responsive: true,
        maintainAspectRatio: false,
        title: {
          display: true,
          text: "My Data",
        },
      });
    }
    },
    immediate: true
  }
},

I am getting an error:

Uncaught (in promise) TypeError: Cannot read property 'getContext' of undefined

What could it be?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2021-05-15
@yozjke

It could be the lack of canvas - you are trying to render the graph immediately after the component is instantiated, when no DOM elements have been created yet. Wrap the watcher code in nextTick .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question