Nested query in node js using mysql

前端 未结 3 1516
感动是毒
感动是毒 2021-02-14 20:47

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          


        
3条回答
  •  温柔的废话
    2021-02-14 21:08

    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.

提交回复
热议问题