module.exports = {
getDataFromUserGps: function(callback)
{
connection.connect();
connection.query("SELECT * FROM usergps",
function(err, results, fields) {
if (err) return callback(err, null);
return callback(null, results);
}
);
connection.end();
},
loginUser: function(login, pass, callback)
{
connection.connect();
connection.query(
"SELECT id FROM users WHERE login = ?AND pass = ?",
[login, pass],
function(err, results, fields)
{
if (err) return callback(err, null);
return callback(null, results);
}
);
connection.end();
},
getUserDetails: function(userid, callback)
{
connection.connect();
connection.query(
"SELECT * FROM userProfilDetails LEFT JOIN tags ON userProfilDetails.userId = tags.userId WHERE userProfilDetails.userid = ?",
[userid],
function(err, results, fields)
{
if (err) return callback(err, null);
return callback(null, results);
}
);
connection.end();
},
addTags: function(userId, tags)
{
connection.connect();
connection.query(
"INSERT INTO tag (userId, tag) VALUES (?, ?)",
[userId, tags],
function(err, results, fields)
{
if (err) throw err;
}
)
connection.end();
}
}
Everything worked fine at first, but when I executed the second “query, “I got this error:
Cannot enqueue Handshake after invoking quit
I’ve tried turning off the connection without using the.end() method, but it doesn’t work.
Thanked first.
Radex
Those blind solutions and water paste I will not translate.
According to:
Fixing Node Mysql “Error: Cannot enqueue Handshake after invoking quit.”:
http://codetheory.in/fixing-node-mysql-error-cannot-enqueue-handshake-after-invoking-quit/
TL; Every time DR closes a connection you need to create a new connection using the createConnection method.
And
Note: If you are serving web requests, you should not turn off the connection each time the request is processed. When the server starts up, create a connection and keep querying it with the Connection/Client object. To handle server disconnection and reconnection events you can listen for error events. Complete code: Here.
Again according to:
The Readme. Md – Server disconnects:
https://github.com/felixge/node-mysql#server-disconnects
It said
Server disconnects
You may lose your connection to MySQL server due to a network problem, server timeout, or server hanging. All of these are considered “fatal errors “and there will be an error codeerr. Code = 'PROTOCOL_CONNECTION_LOST'
. See the error handling section for more information.
The best way to handle these unwanted disconnections is as follows:function handleDisconnect(connection) { connection.on('error', function(err) { if (!err.fatal) { return; } if (err.code !== 'PROTOCOL_CONNECTION_LOST') { throw err; } console.log('Re-connecting lost connection: ' + err.stack); connection = mysql.createConnection(connection.config); handleDisconnect(connection); connection.connect(); }); } handleDisconnect(connection);
As the example above shows, reconnection is achieved by creating a new connection, because the connection object is designed so that it cannot be reconnected once it dies.
When a connection pool is used, suspended connections are removed from the pool and space is freed, and a new connection is automatically created when a new connection request arrives.
the respondent has posted his own autoreconnect code at the end, so I’m not going to post it here.The answer was 18:58 on May 3, 2013
XP1
Although this answer was not adopted by the main topic, but I and the following comments have always thought that this answer is better.
The original web site: http://stackoverflow.com/questions/14087924/cannot-enqueue-handshake-after-invoking-quit
Read More:
- [Solved] gitbook: node_modules\npm\node_modules\graceful-fs\polyfills.js:287
- node.js Server MySQL database connection timeout (error: connect etimeout)
- ERROR in ./node_modules/css-loader/dist/cjs.js!./node_modules/less-loader/dist/cjs.js!./src/css/spec
- Solve the problem that node. js USES MySQL to connect econnunion 127.0.0.0.1:3306
- Node.js Cannot find module ‘mysql’ ‘express’
- Debugging with chrome Node.js
- After node.js is installed, use the instruction node version in vscode to show that it is not an external or internal instruction. The solution is as follows:
- NPM and Node.js Version incompatibility
- Node.js Error: cannot find module ‘xxx’
- Copy node causes NPM execution error: cannot find module ‘/ / lib / utils/ unsupported.js ‘
- Install and uninstall under Linux system Node.js
- Node.js Using port 80 to report errors in Linux
- Events.js:167 throw er appears when starting node service under linux; // Unhandled ‘error’ event solution
- Error 2052/2053 when installing node.js in win
- node.js Static resource acquisition failed
- node.js Cannot find module “XXX” solution
- node.js Error: write epipe problem solved!
- Node.js Medium package.json The difference between devdependences and dependencies
- How to Fix Node.js connection to MYSQL error.
- MySQL restart error the server quit without updating PID file