Node Mysql Cannot Enqueue a query after calling quit Node Mysql Cannot Enqueue a query after calling quit express express

Node Mysql Cannot Enqueue a query after calling quit


I fixed this problem use this method:

connection.end() in your connection.query function

The fixed code is here


If you're using the node-mysql module by felixge then you can call connection.end() at any point after you've made all of the connection.query() calls, since it will wait for all of the queries to finish before it terminates the connection.

See the example here for more information.

If you're wanting to run lots of queries in series, you should look into the async module, it's great for dealing with a series of asynchronous functions (i.e. those that have a callback).


Maybe the problem is that the mySQL query is executed after the connection is already closed, due to the asynchronous nature of Node. Try using this code to call connection.end() right before the thread exits:

function exitHandler(options, err) {    connection.end();    if (options.cleanup)        console.log('clean');    if (err)        console.log(err.stack);    if (options.exit)        process.exit();}//do something when app is closingprocess.on('exit', exitHandler.bind(null, {cleanup: true}));

Code adapted from @Emil Condrea, doing a cleanup action just before node.js exits