question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`Uncaught TypeError: Cannot read property 'length' of null` (vuejs + chart.js)

See original GitHub issue

Hello,

I am trying to use Chart.js with vuejs. Unfortunately, I’m already blocked from trying out the “beginners” example’…

Here’s my Vue Component:

<template>
  <canvas id="myChart" width="400" height="400"></canvas>
</template>

<script>
/*eslint-disable*/
import Chart from '../../node_modules/chart.js/src/Chart.js'
let ctx = document.getElementById("myChart")

var myChart = new Chart(ctx, {
    type: 'bar',
    data: {
        labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
        datasets: [{
            label: '# of Votes',
            data: [12, 19, 3, 5, 2, 3]
        }]
    },
    options: {
        scales: {
            yAxes: [{
                ticks: {
                    beginAtZero:true
                }
            }]
        }
    }
});

export default {
  name: 'dashboard',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'
    }
  }
}
</script>

And the error I get: Uncaught TypeError: Cannot read property 'length' of null

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:17 (4 by maintainers)

github_iconTop GitHub Comments

27reactions
Kinzowacommented, Mar 17, 2017

Oh I see, <canvas>...</canvas> need to be declared before <script>...</script>

👍

6reactions
gkatsanoscommented, Mar 16, 2017

@HornKild read my last/previous comment. The error was due to the element not being present in the HTML when Chart.js was trying to attach the chart on it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Chart Js Cannot read property 'length' of undefined
The problem is that when your code executes, the canvas has not been created yet. You should wrap your code inside a function...
Read more >
Cannot read property 'length' of null"-Chart.js - appsloveworld
[Solved]-Problems with ChartJS "Uncaught TypeError: Cannot read property 'length' of null"-Chart. js. I believe that the answer is that the tag appears after ......
Read more >
Uncaught Typeerror: Cannot Read Property 'Length' Of Null
This error indicates that our code expects to have an object with a length property but that object was not present. length is...
Read more >
JS error:Cannot read property 'length' of undefined
JS error:Cannot read property 'length' of undefined; Debugging error in Google Charts · Ravi Panchumarthy · asgallant.
Read more >
Uncaught TypeError cannot read property 'addeventlistener' of ...
In JavaScript, a very common error is the Uncaught TypeError Cannot read property 'addeventlistener' of null. This error occurs when JavaScript is not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found