-
Notifications
You must be signed in to change notification settings - Fork 11.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uncaught TypeError: Cannot read property 'length' of null
(vuejs + chart.js)
#3715
Comments
Are you able to make a fiddle with this error so that we can debug? |
The chart initialization should have been in mounted / the element was missing |
I have the same problem, how to you solve it ? |
@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. |
I'm very confused because I created a new HTML page with a clean code, no element missing, and still I get this error.
And the error :
What could be wrong with this ? Thank you, |
Oh I see, 👍 |
Hit the same issue and yes the |
Same issue here expect I am dynamically creating everything does this mean that the canvas must be appended to the DOM before instantiating the Chart ? |
@AKFourSeven you can't create a chart without a valid DOM reference (the canvas element must exist before constructing the chart), however the element doesn't need to be attached to the DOM (see #4591). That's a recent feature (v2.7), maybe you are not using the latest version. Note: if you really need to import your scripts before your canvas element, you can still delay the chart creation until the DOM is fully loaded (jsfiddle): <script>
window.addEventListener('load', function() {
new Chart('chart', {
type: 'line',
data: // ...
});
});
</script>
<canvas id="chart"></canvas> |
Isn't there another way? this will prove to be very painful with my current code, the canvas and the chart both have to be loaded before the page is fully loaded, there is no two way around it. |
Maybe I didn't understand your issue, but in all cases you can't create a chart without a canvas element (I'm open to any implementation suggestion to do differently). How about creating your canvas dynamically as well (jsfiddle)?: function create_chart() {
// Create a new canvas to receive the chart
var canvas = document.createElement('canvas');
// Attach the canvas wherever you want
// Note: since 2.7, the canvas can be detached when creating the chart
document.body.appendChild(canvas);
// Create the chart on that canvas
var chart = new Chart(canvas, { ... });
} |
Actually I found the culprit, we have a legacy library: prototype (which I cannot simply trash) that is causing the issue. |
Did you try to upgrade to prototype 1.7.3 (https://cdnjs.cloudflare.com/ajax/libs/prototype/1.7.3/prototype.min.js), seems to work fine with Chart.js 2.7 (fiddle). |
Just tried that, well I will see if upgrading (or getting rid of the lib altogether is possible). |
do not put the chart in created .....just put it in mounted . |
I understand this is due to the vue life cycle, right? |
Mounted triggered after first render but created triggered before the rendering. |
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:
And the error I get:
Uncaught TypeError: Cannot read property 'length' of null
The text was updated successfully, but these errors were encountered: