papaparse使用踩坑
papaparse使用踩坑
-
使用fetch(filepath)读取文件
注意:代码中result.value 和result只用了其中一行。import Papa from 'papaparse'; async function fetchCsv() { const response = await fetch('/data/nodes.csv') const reader = response.body.getReader() const result = await reader.read() // raw array const decoder = new TextDecoder('utf-8'); const csv = decoder.decode(result.value) // 获得结果是一堆奇怪的html代码,不是预期数组 const csv = decoder.decode(result) // 错误出现在该行,错误详情放下面了。 const results = Papa.parse(csv, { header: true }) // object with { data, errors, meta } const rows = results.data // array of objects }
错误详情:Uncaught TypeError: Failed to execute 'decode' on 'TextDecoder': The provided value is not of type '(ArrayBuffer or ArrayBufferView)'
-
直接把文件路径写parse方法里对我也不行,也是出现一堆奇怪的html代码,非预期数组
import Papa from 'papaparse'; fetchCsv = ()=>{ let data; Papa.parse('/data/nodes.csv',{ download: true, dynamicTyping: true, complete: function(results){ data = results.data; } }) }
-
用require获取csv文件可行👍
import Papa from 'papaparse'; fetchCsv = ()=>{ var csvFilePath = require("/data/nodes.csv"); Papa.parse(csvFilePath,{ download: true, dynamicTyping: true, complete: function(results){ data = results.data; } }); }