How to import csv file into mysql using node.js?

Now I'm trying using the fast-csv library to do this:

var stream = fs.createReadStream("./google.csv"); csv .fromStream(stream, {headers : ["Name","E-mail 1 - Value"], ignoreEmpty: true}) .on("data", function(data){ console.log(data); }) .on("end", function(){ console.log("done"); }); 

But this generates this error: "column header mismatch expected: 2 columns received: 57"

Do you know how I can avoid this? should use a different library / approach

Another problem I am facing is that I am getting the result in hexadecimal ... how can I parse it correctly?

+5
source share
1 answer

You can use the node module 'csv-parse'.

  • read csv file with node 'fs' module
  • pass the data to the csv parser and you will get an array from the array, where the internal array represents each row.

Take a look at the following code.

 var csvParser = require('csv-parse'); fs.readFile(filePath, { encoding: 'utf-8' }, function(err, csvData) { if (err) { console.log(err); } csvParser(csvData, { delimiter: ',' }, function(err, data) { if (err) { console.log(err); } else { console.log(data); } }); }); 

Here filePath is the path to the csv file and the separator will be according to your file. This is the character that separates the fields in the csv file (maybe ",", ".", Etc.).

+4
source

Source: https://habr.com/ru/post/977435/


All Articles