jonschlinkert / parse-csv

CSV parser for node.js
MIT License
15 stars 4 forks source link

renderer .html error #3

Closed mbazouz closed 7 years ago

mbazouz commented 7 years ago

Hello,

I am trying to convert csv to html but always I have the same problem in renderer.js:317. I followed the same example in https://www.npmjs.com/package/parse-csv#available-renderers but always the same output. Output: /home/.../node_modules/parse-csv/lib/renderer.js:317 var numRows = data.length; ^

TypeError: Cannot read property 'length' of undefined at Renderer.html (/home/.../node_modules/parse-csv/lib/renderer.js:317:21) at fs.readFile (/home/.../server.js:101:37) at tryToString (fs.js:456:3) at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:443:12)

doowb commented 7 years ago

The example is missing the parse part. You'll have to parse the string before passing that to a renderer:

var csv = require('parse-csv');
var parser = new csv.Parser();
var renderer = new csv.Renderer();

var str = `
id,fruit,vegetable
1,apple,carrot
2,orange,corn
3,banana,potato`;

var datagrid = parser.parse(str); 
var html = renderer.html(datagrid, {headers: {included: true}});
console.log(html);

You can also simplify this by using csv directly:

var csv = require('parse-csv');
var str = `
id,fruit,vegetable
1,apple,carrot
2,orange,corn
3,banana,potato`;

var html = csv('html', str, {headers: {included: true}});
console.log(html);