M
M
Maxim Nine2017-03-22 14:03:22
MySQL
Maxim Nine, 2017-03-22 14:03:22

Error: Cannot enqueue Handshake after invoking quit. How to fix?

The code:

var sql = require("mysql");
var connection = sql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'password',
    database: 'chat'
});

app.get('/', function(req, res) {
    connection.connect();
    var loga = "kulonful";
    connection.query('select * from users where login = "'+loga+'"', function(err, result){
        if(err) { connection.end(); res.send('Произошла критическая ошибка'); }
        connection.end();
        res.send(result);
    });
})

The first time everything is smooth, but if you refresh the page, an error is thrown in the console:
events.js:160 thrower
; // Unhandled 'error' event
^
Error: Cannot enqueue Handshake after invoking quit.
at Protocol._validateEnqueue (/root/node_modules/mysql/lib/protocol/Protocol.js:202:16)
at Protocol._enqueue (/root/node_modules/mysql/lib/protocol/Protocol.js:135:13)
at Protocol .handshake (/root/node_modules/mysql/lib/protocol/Protocol.js:52:41)
at Connection.connect (/root/node_modules/mysql/lib/Connection.js:130:18)
at /root/api. js:18:16
at Layer.handle [as handle_request] (/root/node_modules/express/lib/router/layer.js:82:5)
at next (/root/node_modules/express/lib/router/route.js :100:13)
at Route.dispatch (/root/node_modules/express/lib/router/route.js:81:3)
at Layer.handle [as handle_request] (/root/node_modules/express/lib/router/layer.js:82: 5)
at /root/node_modules/express/lib/router/index.js:235:24

What's wrong? Help)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yustas Alexu, 2017-03-22
@kulonful

The error occurs because the connection is terminated after the first request. It is necessary to do sql.createConnection again. But this is the wrong approach. The code needs to be rewritten.
Create a connection outside of the request and listen for the error event. If there is an error, break the connection and reconnect using createConnection.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question