Var*_*dan 4 javascript node.js
当我在node.js 中关闭数据库连接时出现此错误
Cannot enqueue Query after invoking quit.
这是我的代码
socket.on('adminConnect', function (email) {
connection = mysql.createConnection(db_config); // db_config has all details of database
connection.connect(); // no problem in connection
connection.query("SELECT id FROM user WHERE email = '" + email + "' ", function (err, results, fields) {
if (err) throw err;
if (results[0]) {
// Some code
connection.end(); // its giving error "Cannot enqueue Query after invoking quit."
} else {
console.log("no id");
}
});
});
Run Code Online (Sandbox Code Playgroud)
通常,您重用连接而不是一直打开/关闭。
要回答您的问题,方法如下:
connection.end();
Run Code Online (Sandbox Code Playgroud)
尝试将该行放在回调之外,因为在结束连接之前所有查询都必须完成,因此您可以像这样安全:
connection.query(.......);
connection.end();
Run Code Online (Sandbox Code Playgroud)
您的代码将是:
socket.on('adminConnect', function (email) {
connection = mysql.createConnection(db_config); // db_config has all details of database
connection.connect(); // no problem in connection
connection.query("SELECT id FROM user WHERE email = '" + email + "' ", function (err, results, fields) {
if (err) throw err;
if (results[0]) {
// Some code
} else {
console.log("no id");
}
});
connection.end();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22268 次 |
| 最近记录: |