【问题标题】:Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'then')Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'then')
【发布时间】:2022-01-11 19:34:32
【问题描述】:

我正在使用面向对象的图表并根据滚动位置设置数据。 我正在使用 d3.csv() 通过 then() 加载数据,如下所示:

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
                                });

将滚动条库与图表的所有代码一起包含在内对我来说是相当复杂的,所以我希望以上内容足够清楚。 我的主要问题是在then() 中调用init 而不使用 () 会显示图表,但会在标题中返回错误。当我使用括号 (then(init())) 时,出现以下错误:

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

我正在使用 d3.v6 有人可以帮忙吗?

【问题讨论】:

  • .on('active', function(i){console.log(i, dataArr.length, dataArr[i]); .... 所以调试一下看看是怎么回事。

标签: javascript d3.js


【解决方案1】:

不带括号的.then(init) 相当于说.then((array) => init(array)),因为在这两种情况下,您都给.then 一个单参数函数,以便在您的承诺返回时执行。如果错误随后出现在标题中,我猜这部分代码中发生了一些事情,但这里没有。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-09
    • 2022-07-21
    • 2022-07-22
    • 2022-10-13
    • 2022-01-07
    • 2022-10-18
    • 2021-11-10
    • 2022-06-13
    相关资源
    最近更新 更多