actually wrote this code

var csv = require('csv-stream'); var request = require('request'); var fs = require('fs'); var pg = require('pg'); var conString = "pg://admin:admin@localhost:5432/labels"; // All of these arguments are optional. var options = { delimiter : ';', // default is , endLine : '\n', // default is \n, escapeChar : '"', // default is an empty string enclosedChar : '"', // default is an empty string } try { var csvStream = csv.createStream(options); fs.createReadStream('personss.csv').pipe(csvStream) .on('error', function (err) { console.error(err); }) .on('data', function (data) { // outputs an object containing a set of key/value pair representing a line found in the csv file. console.log(data); pg.connect(conString, function (err, client, done) { client.query( 'INSERT into test (firstname, lastname) from', function (err, result) { if (err) { console.log(err); } else { console.log('data inserted'); } }); }); }); } catch (e) { console(e.message); }; 

How to write to client.query so that it inserts into the database the data that was written to the data variable in the parser.

Update

 pg.connect(conString, function (err, client, done) { var sql = 'INSERT into test (firstname, lastname) VALUES ("' + data.firstname + '","' + data.lastname + '")' client.query(sql, function (err, result) { if (err) { console.log(err); } else { console.log('data inserted' + result.data[0]); client.end(); } }); }); 
  • one
    github.com/brianc/… - nörbörnën
  • I do not understand how he does so that it records what has passed through the stream into a variable. I begin , roughly speaking, with small steps I go forward - t0rick
  • then read the first link, do forEach by data and client.query inside - nörbörnën
  • one
    the js cycle is written incorrectly, no data is inserted into the sql (although the first link clearly shows how to do it). read at least some introductory books on these languages. - nörbörnën

1 answer 1

  pg.connect(conString, function (err, client, done) { var sql = "INSERT into test (firstname, lastname) VALUES ('" + data.firstname + "','" + data.lastname + "')" client.query(sql, function (err, result) { if (err) { console.log(err); } else { console.log('data inserted' + result.data[0]); client.end(); } }); }); 

UPDATE 1.0

I will say one thing, you need to follow the quotes