Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested query in node js using mysql

Tags:

node.js

mysql

I am trying following code at node js using mysql but getting error "Cannot enqueue Query after invoking quit.

var mysql = require('mysql');

var connection = mysql.createConnection({
host     : 'localhost',
user     : 'USER',
password : 'PASS',
database : 'DB',
});

connection.connect();

var queryString = 'SELECT * FROM tbl_product';

connection.query(queryString, function(err, rows, fields) {
    if (err) throw err;
    for (var i in rows) {
        console.log('Product Name: ', rows[i].product_name);
        var emp_query = 'SELECT * FROM tbl_employer';
        connection.query(queryString, function(emp_err, emp_rows, emp_fields) {
            if (emp_err) throw emp_err;
            for (var e in emp_rows) {
                console.log('Employer Name: ', emp_rows[e].company_name);
            }
        }); 
    }
});  
connection.end(); 
like image 241
user3699262 Avatar asked Mar 30 '15 11:03

user3699262


3 Answers

I see two problems in your code:

  • You're calling connection.end() synchronously, but your queries run in a asynchronous flow. You have to call connection.end() only when you've finished the second query.
  • You're using a regular for loop to run assynchronous calls (you outter loop).

To accomplish what you're trying to do, you have to consider those assynchronous scenarios. You could use promises or a module like async, that provides you a lot of methods to deal with assyncronous flows, like async.each():

connection.query(queryString, function(err, rows, fields) {
    if (err) throw err;

    async.each(rows, function (row, callback) {
        console.log('Product Name: ', row.product_name);
        var emp_query = 'SELECT * FROM tbl_employer';
        connection.query(queryString, function(emp_err, emp_rows, emp_fields) {
            if (emp_err) callback(emp_err);
            for (var e in emp_rows) {
                console.log('Employer Name: ', emp_rows[e].company_name);
            }
            callback();
        }); 
    });
    }, function (err) {
        connection.end();
    }
});

Now it will guarantee that connection.end() will just be called when all your queries have finished.

like image 130
Rodrigo Medeiros Avatar answered Oct 06 '22 12:10

Rodrigo Medeiros


Remove connection end function

==> connection.end();

like image 20
som Avatar answered Oct 06 '22 13:10

som


The problem is connection.end() triggered before your query is not finished yet. Try to put connection.end() to end of outer loop.

connection.query(queryString, function(err, rows, fields) {
    if (err) throw err;
    for (var i in rows) {
        console.log('Product Name: ', rows[i].product_name);
        var emp_query = 'SELECT * FROM tbl_employer';
        connection.query(queryString, function(emp_err, emp_rows, emp_fields) {
            if (emp_err) throw emp_err;
            for (var e in emp_rows) {
                console.log('Employer Name: ', emp_rows[e].company_name);
            }
        }); 
    }
    connection.end();
});

Hope it will be useful for you.

like image 45
Arkar Aung Avatar answered Oct 06 '22 13:10

Arkar Aung