我是异步编码的新手,正在疯狂地尝试着找出一个解决方案,将数据从嵌套的json数组文件加载到Chart JS中。 梳理所有不同的示例,我可以获得fetch调用内部的数据,但无法将其输出到全局对象中,因为它是异步的。 目标是引用图表配置中的json字段和值。 下面是当前的迭代。。。
null
var jsonData =
fetch('dataSummary.json')
.then(status)
.then(json)
.then(function(data) {
console.log('Request succeeded with JSON response', data[0]);
return data[0]
}).catch(function(error) {
console.log('Request failed', error);
});
var chartData = {jsonData:{}};
Promise.all([jsonData]).then(function(values){
chartData = values[0];
console.log(chartData);
return chartData[0];
});
console.log(Object.keys(chartData))
null
谢谢
关键是在数据准备就绪时更新(或绘制)图表。
fetch('dataSummary.json')
.then(res => json)
.then(function(data) {
console.log('Request succeeded with JSON response', data[0]);
// rather the returning data use it to update/populate the chart
}).catch(function(error) {
console.log('Request failed', error);
});