In my code, I have two classes that utilize d3. The first class is Main.js, which creates an instance of another class called Data.js. This Data class is responsible for loading data from a csv file and then applying d3.nest to it. However, I am encountering an "Uncaught TypeError: Cannot read property 'then' of undefined." error when trying to use a promise in the main class.
Below is the relevant code snippet from the main class:
this.data = new Data();
this.mainChartData = this.data.loadNestedData().then(nestedData => { // error
console.log(nestedData);
});
And here is the implementation of the Data class:
class Data {
constructor() {}
loadNestedData() { d3.csv("data.csv").then(loadedData => {
console.log(loadedData); // this logs correctly
this.nestedData = d3.nest()
.key(function(d) { return d.gameID; })
.entries(loadedData);
console.log(this.nestedData); // this also logs correctly
return this.nestedData;
});
}