Edg*_*jak 2 javascript asynchronous promise d3.js
我正在使用d3-fetch库以能够使用异步/等待,但是出现以下错误:fetch.json不是一个函数。
它确实与d3.json()一起工作。
我在这里做错了什么?
async function fetchAllData() {
try {
let data = await fetch.json('https://api.myjson.com/bins/1cpi9w');
return data;
console.log("Initial data: ", data);
console.log("type of n: ", indicateType(data[0].n));
let format = d3.timeFormat("%Y");
//Nesting data by category
let updatedData = d3.nest()
.key(d => d.category)
.sortValues((a, b) => a.year - b.year)
.entries(data);
//Define xScale domain (min,max assume it has been sorted by year)
xScale.domain([
d3.min(updatedData, function(s) { return s.values[0].year; }),
d3.max(updatedData, function(s) { return s.values[s.values.length - 1].year })
]);
console.log("Nested data: ", updatedData);
//Draw an SVG for each country
} catch (error) {
console.log(error);
}
}
const indicateType = unevaluatedOperand => typeof unevaluatedOperand;
fetchAllData();
Run Code Online (Sandbox Code Playgroud)
提取是Web API的一部分。根据文档,调用fetch.json()不是它的工作方式。
这是一个工作示例:
async function fetchAllData () {
// await response of a fetch call
const response = await fetch('https://api.myjson.com/bins/1cpi9w');
// once the promise is resolved convert the response to an object
const data = await response.json();
// your data has been converted to an object now)
console.log('Initial data:', data);
return data;
}
fetchAllData();Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
496 次 |
| 最近记录: |