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.

TypeError: Cannot read properties of undefined

See original GitHub issue

I am getting the following errors:

TypeError: Cannot read properties of undefined (reading 'visible')

where the error references the following line and function:

_getSortedDatasetMetas(filterVisible) {
     const me = this;
     const metasets = me._sortedMetasets;
     const result = [];
     let i, ilen;
     for (i = 0, ilen = metasets.length; i < ilen; ++i) {
       const meta = metasets[i];
       if (!filterVisible || meta.visible) { <---- THIS LINE
         result.push(meta);
       }
     }
     return result;
    }

TypeError: Cannot read properties of undefined (reading 'controller')

where the error references the following line:

const style = meta.controller.getStyle(usePointStyle ? 0 : undefined);

The problem is that it happens randomly and I can not find any culprit for the problem.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:22

github_iconTop GitHub Comments

6reactions
chrmcgcommented, Nov 4, 2021

This is happening for me on react-chartjs-2@3.3.0 with chart.js@3.6.0.

It seems that when the data prop passed into <Line> changes, this can happen and has something to do with missing metasets in Chart.js/src/core/core.controller.js:_getSortedDatasetMetas:

image (note the empty ⨉ 4 marker)

I was able to mitigate by passing redraw={true}: <Line data={data} options={options} redraw />

But this means I lose out on the cool animation between old data and new data.

@dangreen This is a total guess, but could it be possible that the performance optimizations around here, something about the mutation of old datasets to produce new datasets, are what cause the metasets to be dropped on the Chart.js side? It seems like Chart.js 3.6.0 does use referential equality on datasets to perform some kind of optimization on their end.

Thanks for this amazing package, hoping this can be a helpful pointer to the bug!

3reactions
mciszczoncommented, Jan 18, 2022

I had the same problem with "chart.js": "3.6.2" and "react-chartjs-2": "4.0.0", but only in Jest tests. The app was working fine, but the tests would fail with this:


    TypeError: Cannot read property 'prototype' of undefined

      33 | import { Chart as ReactChart } from 'react-chartjs-2'
      34 |
    > 35 | ChartJS.register(
         |         ^
      36 |   ArcElement,
      37 |   LineElement,
      38 |   BarElement,

      at TypedRegistry.isForType (node_modules/chart.js/dist/chart.js:6005:74)
      at Registry._getRegistryForType (node_modules/chart.js/dist/chart.js:6148:15)
      at node_modules/chart.js/dist/chart.js:6128:41
          at Array.forEach (<anonymous>)
      at Registry._each (node_modules/chart.js/dist/chart.js:6127:15)
      at Registry.add (node_modules/chart.js/dist/chart.js:6085:10)
      at Function.register (node_modules/chart.js/dist/chart.js:7407:16)
      ...

I found out that the issue is fixed if I move the ChartJS.register() call to be in the body component, and not outside of it, i.e.

const Component = () => {
	ChartJS.register()
	...
}

instead of

ChartJS.register()

const Component = () => {
	...
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot Read Property of Undefined in JavaScript - Rollbar
TypeError: Cannot read property of undefined occurs when a property is read or a function is called on an undefined variable.
Read more >
Uncaught TypeError: Cannot read property of undefined In
JavaScript TypeError is thrown when an operand or argument passed to a function is incompatible with the type expected by that operator or...
Read more >
[Solved] Cannot read Properties of Undefined in JavaScript
The "Cannot read properties of undefined" error occurs when you try to access a property or a method on a variable that stores...
Read more >
Uncaught TypeError : Cannot read properties of undefined
The solution to TypeError: Cannot read properties of undefined ... The root cause of the error is that the declared variable doesn't have...
Read more >
Uncaught TypeError: Cannot read property 'value' of undefined
@TetraDev 'e' will return 'undefined' unless it's a valid TypeError. So you validate the TypeError in the 'if' statement and you can also ......
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