Home > other >  Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '0') -- when
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '0') -- when

Time:11-09

Working on data visualization with d3.js. I'm quite a novice in javascript, but I come here trying to get to the root of my issue.

I'm trying to plot some movie-based data, in which every data point is the string data type. Therefore, I have to convert dates to the Date data type, and Movie Ratings to Number or Float data type for d3 to plot the data properly, else there's an error.

Pretty much, the data is stored in an array, which holds 11 other arrays, one for each movie critic, in which each movie critic array holds 237 objects for each movie watched that year, in which each object looks like

    {
    "key": "Derek",
    "Date": "2019-01-25T05:00:00.000Z",
    "value": 5,
    "Movie": "The Butterfly Effect",
    "Pickedby": "Dan"
} 

Where key is the movie critic, and Pickedby is the person who picked the movie.

Here is the code I'm using to convert a date like 1/25/2019 into "2019-01-25T05:00:00.000Z" and am trying to convert the value item (from a string) into a Number, or Floating Point Number. (Some ratings can be like 3.5, 6.5, etc.)

for (let j = 0; j < series.length; j  ) {
    for (let i = 0; i <series[j].length; i  ){

      series[j][i].Date = new Date(series[j][i].Date)
      series[j][i].value = Number(series[i][j].value)
    
    }
}

The date conversion line of code works as expected. However, converting the value item into a Number or Float is causing Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '0') error to arise.

The thing is, all of the value items data through the entire array is a string. There isn't an undefined issue. Sure, some values are (x's) or some other alphabetical character, but that should be converted to NaN at least, and it is for some.

Any thoughts on why this error could be arising, given the information I have provided? I can always probably provide more detail.

Thanks!

CodePudding user response:

It looks like a typo was made in the code, where you are not accessing the correct value in the cast to Number.

Instead of:

series[j][i].value = Number(series[i][j].value)

it should be:

series[j][i].value = Number(series[j][i].value)
  • Related