1

I'm trying to parse file with data and insert it in the database, parsing goes well and when I make one query it runs great to, but when I trying to get this queries work in cycle it give me

values: [output[i][0], output[i][1], output[i][2], output[i][3], o
                  ^
TypeError: Cannot read property '0' of undefined

First I was trying simple for loop but after some research about nodeJS being async I find that i need to do this in callback, but it didn't works, I think that parsing is not done by moment of starting queries routine, but I'm not sure.

var express = require('express');
var pg = require('pg');
var csv = require('fast-csv');
var app = express();

var conString = "postgres://alexzander:,tjdekma@localhost/db2015";
var output = [];

var client = new pg.Client(conString);

parser = csv.fromPath("public/dataInputOld/tblOwner.txt", {delimiter: ';'});
parser.on("data", function (data) {
    output.push(data);
});

parser.on("end", query(1));

function query(i) {
    if (i < 30) {
        client.query({
            text: 'INSERT INTO tblowner ' +
            '(intownerid, txtownersurname, txtownername, txtownersecondname, txtaddress)' +
            ' VALUES ($1, $2, $3, $4, $5)',
            values: [output[i][0], output[i][1], output[i][2], output[i][3], output[i][4]]
        }, function (err) {
            if (err) {
                console.log('error: ' + err)
            }
            else {
                console.log(i);
                query(i + 1);
            }
        });
    }
}

app.get('/', function (req, res) {
        res.send(JSON.stringify(output[1][0]));
});

var server = app.listen(3001, function () {

    var host = server.address().address;
    var port = server.address().port;

    console.log('App listening at http://%s:%s', host, port);
});

Update: now I think that i didn't connect well to database

Update2: I changed

 parser.on("end", query(1));

to

parser.on("end", function(){
    query(1);
});

and now error is gone, but nothing inserts in database

2
  • Try to change parser.on("end", query(1)) to parser.on("end", console.log(output)) to check you have correct data and your conString has a comma after alexander: i don't think it should be there Commented Apr 6, 2015 at 8:30
  • @Molda comma is a part of password and console.log(output): [] events.js:130 throw TypeError('listener must be a function'); he thinks that console.log is not a function? but array is for some reason empty Commented Apr 6, 2015 at 8:37

1 Answer 1

1

Ended with this, sorry but I didn't understand enough about pg and getting client in it, so when i did all like in example on there docs all worked fine, thanks stackoverflow :) If I didn't try to explain this trouble to you I think I might be searching for errors a lot more time.

parser.on("end", function () {
    client.connect(function(err){
        if (err) {
            return console.error('could not connect to postgres', err);
        }
        query(1);
    });
});

function query(i) {
    if (i < 30) {
        client.query({
            text: 'INSERT INTO tblowner ' +
            '(intownerid, txtownersurname, txtownername, txtownersecondname, txtaddress)' +
            ' VALUES ($1, $2, $3, $4, $5)',
            values: [output[i][0], output[i][1], output[i][2], output[i][3], output[i][4]]
        }, function (err) {
            if (err) {
                console.log('error: ' + err)
            }
            else {
                console.log(i);
                query(i + 1);
            }
        });
    }else{
        client.end();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.