Error Background:
When the database operation is performed for the first time, it is successful. The second time a database operation is performed, an error is reported as shown in the title.
The reason:
This is because after we close the connection with the.end() method, we need to re-call createConnection to recreate a connection.
Solutions:
For example, the following encapsulates a method that operates on database queries and additions. Each time a new connection is created using mysql.createconnection () before the database operation is performed, instead of putting it directly to the outermost layer.
var mysql = require('mysql');
exports.find=function(findSql,callback){
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '123456',
port: '3306',
database: 'namesharing',
});
connection.connect();
connection.query(findSql,function (err, result) {
if(err){
console.log('[SELECT ERROR] - ',err.message);
return;
}
connection.end();
callback(result)
});
}
exports.insert=function(addSql,addSqlParams,callback){
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '123456',
port: '3306',
database: 'namesharing',
});
connection.connect();
connection.query(addSql,addSqlParams,function (err, result) {
if(err){
console.log('[INSERT ERROR] - ',err.message);
return;
}
connection.end();
// console.log('INSERT ID:',result);
callback(result)
});
}
Read More:
- MySQL error: starting MySQL… The server quit without updating PID file
- Node js events.js:183 throw er; // Unhandled ‘error’ event
- The reason and solution for the error ECONNRESET of the httpClieint request of Node.js
- [Solved] Error: unable to perform an operation on node ‘rabbit@DESKTOP-xxx‘. Please see diagnostics informa
- [Solved] Rabbitmq Server Error: unable to perform an operation on node ‘rabbit@nscczzms‘. P
- Node.js Error: throw er; // Unhandled ‘error‘ event [How to Solve]
- json-server Error: EPERM: operation not permitted, mkdir ‘C:\Progra m Files\nodejs\node_
- [Solved] internal/modules/cjs/loader.js:892 ^Error: Cannot find module ‘C:\Users\LX\Desktop\Node_DEMO\a
- NPM install error: Cannot find module ‘D:\zip\node_modules\npm\bin\npm-cli.js‘
- Error in invoking target [How to Solve]
- Node Kubelet Error: node “xxxxx“ not found [How to Solve]
- C++ Error: Invoking “make -j2 -l2“ failed [How to Solve]
- Webpack Pack and compress ES6 files with errors: ERROR in js/xxxxxx.js from UglifyJs Unexpected token punc ()
- SpringBoot uses MyBatis error: Error invoking SqlProvider method (tk.mybatis.mapper.provider.base.BaseInsertProvider.dynamicSQL)
- [Solved] catkin_make Error: Invoking “make -j12 -l12“ failed
- [Solved] Oracle 18C RAC Install Error: Error in invoking target ‘irman ioracle idrdactl idrdalsnr idrdaproc‘ of makefile
- OpenSSL: error:14077410:SSL routines:SSL23_GET_SERVER_HELLO:sslv3 alert handshake failur
- Rsync Error: Operation not permitted [How to Solve]
- [kubernetes] the pod instance of calico node always reports an error and restarts
- Mysql5.7.18.1 Error 1054 (42S22): Unknown Column’password’ In’field List’ When Changing User Password