【发布时间】:2018-05-08 22:08:21
【问题描述】:
我正在尝试使用我的 react App.js 文件中的 d3-request 库来导入和解析 .csv 文件中的数据。当我将结果数据输出到控制台时,它会从 App.js 文件而不是目标 csv 文件返回一组行。我错过了什么?
App.js
import React, {Component} from 'react';
import {csv} from 'd3-request';
import './App.css';
import Chart from './components/chart'
const API_URL = "https://nataliia-radina.github.io/react-vis-example/";
class App extends Component {
constructor(props) {
super(props)
this.state = {
results: [],
};
}
render() {
csv("./data/test.csv", function(error, data) {
if (error) throw error;
console.log(data);
});
const {results} = this.state;
return (
<div className="App">
<Chart data={results}
/>
</div>
);
}
}
export default App;
test.csv
col1,col2,col3
hi,there,buddy
控制台输出
(39) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, columns: Array(1)]
0:{<!DOCTYPE html>: "<html lang="en">"}
1:{<!DOCTYPE html>: " <head>"}
2:{<!DOCTYPE html>: " <meta charset="utf-8">"}
3:{<!DOCTYPE html>: " <meta name="viewport" content="width=device-width"}
4:{<!DOCTYPE html>: " <meta name="theme-color" content="#000000">"}
5:{<!DOCTYPE html>: " <link rel="manifest" href="/manifest.json">"}
6:{<!DOCTYPE html>: " <link rel="shortcut icon" href="/favicon.ico">"}
7:{<!DOCTYPE html>: " <title>React App</title>"}
8:{<!DOCTYPE html>: " </head>"}
9:{<!DOCTYPE html>: " <body>"}
10:{<!DOCTYPE html>: " <noscript>"}
11:{<!DOCTYPE html>: " You need to enable JavaScript to run this app."}
12:{<!DOCTYPE html>: " </noscript>"}
13:{<!DOCTYPE html>: " <div id="root"></div>"}
14:{<!DOCTYPE html>: " <script type="text/javascript" src="/static/js/bundle.js"></script></body>"}
15:{<!DOCTYPE html>: "</html>"}
columns:["<!DOCTYPE html>"]
length:39
__proto__:Array(0)
【问题讨论】:
-
我认为它找不到 CSV 文件(即 404 错误),并且您的服务器设置为在这种情况下返回
index.html文件(允许客户端路由的常见配置)。您在浏览器控制台中看到了什么? -
浏览器控制台输出如上在
console output下的我的OP中。它显示了 index.html 文件。如果找不到我的文件,它默认为索引是有道理的。但是我的文件在那里,所以我不明白为什么要这样做。 -
您使用的是 D3 v3/4 还是 D3 v5?
-
@dbconfession 抱歉,我应该更清楚一点,我的意思是“网络”选项卡。
标签: javascript reactjs csv d3.js