node.js连接mysql报错Error: Cannot enqueue Handshake after invoking quit.
第一次访问时一切正常不会发生错误,刷新之后就会发生如下错误
Error: Cannot enqueue Handshake after invoking quit.
at Protocol._validateEnqueue (D:\CodeWorkspace\expressmysql\node_modules\mysql\lib\protocol\Protocol.js:215:16)
at Protocol._enqueue (D:\CodeWorkspace\expressmysql\node_modules\mysql\lib\protocol\Protocol.js:138:13)
at Protocol.handshake (D:\CodeWorkspace\expressmysql\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (D:\CodeWorkspace\expressmysql\node_modules\mysql\lib\Connection.js:119:18)
at D:\CodeWorkspace\expressmysql\src\com\ftg\controller\dept.js:6:8
at Layer.handle [as handle_request] (D:\CodeWorkspace\expressmysql\node_modules\express\lib\router\layer.js:95:5)
at next (D:\CodeWorkspace\expressmysql\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (D:\CodeWorkspace\expressmysql\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\CodeWorkspace\expressmysql\node_modules\express\lib\router\layer.js:95:5)
at D:\CodeWorkspace\expressmysql\node_modules\express\lib\router\index.js:281:22
Emitted 'error' event at:
at Connection._handleProtocolError (D:\CodeWorkspace\expressmysql\node_modules\mysql\lib\Connection.js:426:8)
at Protocol.emit (events.js:189:13)
at Protocol._delegateError (D:\CodeWorkspace\expressmysql\node_modules\mysql\lib\protocol\Protocol.js:398:10)
at Handshake.<anonymous> (D:\CodeWorkspace\expressmysql\node_modules\mysql\lib\protocol\Protocol.js:232:10)
at Handshake.emit (events.js:189:13)
at Handshake.Sequence.end (D:\CodeWorkspace\expressmysql\node_modules\mysql\lib\protocol\sequences\Sequence.js:78:12)
at D:\CodeWorkspace\expressmysql\node_modules\mysql\lib\protocol\Protocol.js:236:14
at process._tickCallback (internal/process/next_tick.js:61:11)
连接mysql 然后不断开,一直连着数据库,这样就不会错了,
route_dept.get('/show',function(req,res){
db.connect(); (把这行移动到方法外)
db.query("select * from dept",function (error,results, fields){
if(error) throw error;
var json = {
code:0
,msg:'成功'
,data:results
};
res.send(json); // send end({aa:123})....
db.end(); (删除这行)
});
});