当我关闭数据库连接时node.js我收到这个错误
Cannot enqueue Query after invoking quit
.
这是我的code
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");
}
});
});
一般来说,您会重复使用连接,而不是一直打开/关闭。
要回答你的问题,方法如下:
connection.end();
尝试将该行放在回调之外,因为所有查询都必须在结束连接之前完成,因此您是安全的:
connection.query(.......);
connection.end();
那么你的代码将是:
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();
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)