Languages

Node.js - MySQL - how to close connection? (Cannot enqueue Query after invoking quit error)

0 points
Asked by:
Dirask JS Member
3430

I tried to query the MySQL database in Node.js but I don't know where to end the connection.

When I call the end method at the end of the code it gives me an error.

My code: 

connection.connect((err) => {
  if (err) throw err;
  let sql = `SELECT * 
             FROM users
             LIMIT ? OFFSET ?`;
  const value = [2, 1];

  connection.query(sql, value, (err, result) => {
    if (err) throw err;
    console.log(result);
  });
});

connection.end();

Stack trace: 

C:project>node src/App.js
C:project\src\App.js:20
    if (err) throw err;
             ^

Error: Cannot enqueue Query after invoking quit.
    at Protocol._validateEnqueue (project\node_modules\mysql\lib\protocol\Protocol.js:215:16)
    at Protocol._enqueue (project\node_modules\mysql\lib\protocol\Protocol.js:138:13)
    at Connection.query (project\node_modules\mysql\lib\Connection.js:198:25)
    at Handshake.<anonymous> (project\src\App.js:19:7)
    at Handshake.<anonymous> (project\node_modules\mysql\lib\Connection.js:526:10)
    at Handshake._callback (project\node_modules\mysql\lib\Connection.js:488:16)
    at Handshake.Sequence.end (project\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
    at Handshake.Sequence.OkPacket (project\node_modules\mysql\lib\protocol\sequences\Sequence.js:92:8)
    at Protocol._parsePacket (project\node_modules\mysql\lib\protocol\Protocol.js:291:23)
    at Parser._parsePacket (project\node_modules\mysql\lib\protocol\Parser.js:433:10) {
  code: 'PROTOCOL_ENQUEUE_AFTER_QUIT',
  fatal: false
}
0 answers
Native Advertising
­čÜÇ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ÔŁĄ´ŞĆ­čĺ╗ ­čÖé

Join