Home > OS >  Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'then')
Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'then')

Time:01-05

I'm using an object oriented chart and setting data according to the scroll position. I'm using d3.csv() to load data with then(), like this:

const chart = new Chart(instatiatechartfunctions);

let dataObj1 = d3.csv('datapath', dataparsefunction);
let dataObj2 = d3.csv('datapath2', dataparsefunction);

let dataArr = [dataObj1,dataObj2];

function init(data){
     chart.setData(data);
    }

let g3 = scrollerfunction().settings()
                           .on('active', function(i){
                                dataArr[i].then(init); //error here
                                });

It's quite complicated for me to include the scroller library along with all the code for the chart, so I hope the above is clear enough. My main issue is that calling init in then() with no () shows the graph but returns the error in the title. when I use the parentheses (then(init())) I get the following error:

Uncaught TypeError: Cannot read properties of undefined (reading '0')

I am using d3.v6 Can anyone help?

CodePudding user response:

Putting .then(init) without the parenthesis is the equivalent to saying .then((array) => init(array)), as in both cases you are giving .then a one-argument function to execute on the return of your promise. If the error then shows up in the title, I guess that there's something going on in that part of the code, but not here.

  •  Tags:  
  • Related