EN
Node.js - MySQL - how to close connection? (Cannot enqueue Query after invoking quit error)
0 answers
0 points
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:
xxxxxxxxxx
1
connection.connect((err) => {
2
if (err) throw err;
3
let sql = `SELECT *
4
FROM users
5
LIMIT ? OFFSET ?`;
6
const value = [2, 1];
7
8
connection.query(sql, value, (err, result) => {
9
if (err) throw err;
10
console.log(result);
11
});
12
});
13
14
connection.end();
Stack trace:
xxxxxxxxxx
1
C:project>node src/App.js
2
C:project\src\App.js:20
3
if (err) throw err;
4
^
5
6
Error: Cannot enqueue Query after invoking quit.
7
at Protocol._validateEnqueue (project\node_modules\mysql\lib\protocol\Protocol.js:215:16)
8
at Protocol._enqueue (project\node_modules\mysql\lib\protocol\Protocol.js:138:13)
9
at Connection.query (project\node_modules\mysql\lib\Connection.js:198:25)
10
at Handshake.<anonymous> (project\src\App.js:19:7)
11
at Handshake.<anonymous> (project\node_modules\mysql\lib\Connection.js:526:10)
12
at Handshake._callback (project\node_modules\mysql\lib\Connection.js:488:16)
13
at Handshake.Sequence.end (project\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24)
14
at Handshake.Sequence.OkPacket (project\node_modules\mysql\lib\protocol\sequences\Sequence.js:92:8)
15
at Protocol._parsePacket (project\node_modules\mysql\lib\protocol\Protocol.js:291:23)
16
at Parser._parsePacket (project\node_modules\mysql\lib\protocol\Parser.js:433:10) {
17
code: 'PROTOCOL_ENQUEUE_AFTER_QUIT',
18
fatal: false
19
}
0 answers