0

I'm trying to get a node-mysql connection query to work in series with the result of the original query. After all the setup is done, this is the problematic part. It seems that the connection has already ended. How can I ensure that the query in processRow gets executed in tandem with the result of the original?

function processRow(row, callback){
    connection.query('INSERT INTO other_table VALUES (?)', row.id, function(err, rows, fields) {
        if (err) throw err;
    });

    callback();
}

var query = connection.query('SELECT id from tbl limit 2');

query
    .on('error', function(err){
        throw err;
    })
    .on('fields', function(fields){

    })
    .on('result', function(row){
        processRow(row, function(){
            connection.resume();
        });
    })
    .on('end', function(){
        //
    });

connection.end();

https://github.com/felixge/node-mysql

4m1r
  • 12,234
  • 9
  • 46
  • 58

2 Answers2

0
function processRow(row, callback){
    connection.query('INSERT INTO other_table VALUES (?)', row.id, function(err, rows,    fields) {
        if (err) throw err;
        callback();//callback should go here   
    }); 
}

then you could end the connection:

query.on('result', function(row){
    processRow(row, function(){
        connection.end(); //maybe end the connection once insertion into the table is done
    });
 })
azero0
  • 2,220
  • 3
  • 20
  • 31
0

I think the issue with your code that you have to call connection.pause() before calling processRow()

im031985
  • 61
  • 3