gpt4 book ai didi

mysql - Node MySQL 错误 - 错误 : Cannot enqueue Quit after invoking quit

转载 作者:行者123 更新时间:2023-11-29 18:01:06 29 4
gpt4 key购买 nike

我正在创建一个基于 promise 的 API。如果我一次发出一个请求,一切都会正常,但是,如果 2 个或更多请求在一秒内到达服务器,我会收到以下错误

Error: Cannot enqueue Quit after invoking quit

奇怪的是,我得到了返回到 API 请求的有效且正确的结果,但是,显然有些问题。

我创建了一个 small project在 GitHub 上。您可以查看一下,我创建了一个包含 2 个用户的 SQL 播种器,因此您只需将查询复制粘贴到您的数据库中(该播种器甚至会创建一个新数据库)

项目很小,只需将数据库凭据放入 configs/mysql.js 文件中,然后运行该文件 node multiple_sends.js

您将看到返回了正确的答案,但 Node 抛出了错误。

我不认为错误出在 mysql 模块中。原因是,如果我从模型中获取代码并创建 CLI 脚本,则不会返回任何错误,mysql 不会发出任何警告

// models/user.js
let mysql = require('mysql');

let configs = {
host : '127.0.0.1',
user : 'root',
password : 'something',
database : 'demo'
}

/**
* This constructor function is responsible for running queries against
* the mysql database
*/
function Users()
{
// These are PRIVATE variables, they can only be accessed within this class
let mysql_connection = null;

this.getUserByEmail = function(email)
{
let parent = this;

mysql_connection = mysql.createConnection(configs);

return this.mysqlConnect()
.then((connectionId) =>
{
console.log(`getUserByEmail: connection id: ${connectionId}`);

let sql = 'SELECT user_id, first_name, last_name, upassword, email, '
+ 'birthday, roles FROM users WHERE email = ?';

return parent.runQuery(sql, [email]);
})
.then((results) =>
{
if (results.resultSet.length === 0) {
return null;
}

let resultSet = results.resultSet[0];

// return our result to the calling method.
return (resultSet);
})
.catch(error => {
throw error;
})
}

/**
* Run this method when trying to connect to the database
* @param Object error
* @return Promise
*/
this.mysqlConnect = function()
{
return new Promise(function(resolve, reject)
{
mysql_connection.connect(function(error)
{
if (error) {
console.log('Error making connection', error);

throw "Failed connection to database";
}

// we don't have to return anything, this is just to show that Promises
// can return
resolve(mysql_connection.threadId);
});
});
}

/**
* use this method for running selects
* @param string query - well formed query
* @return Promise
*/
this.runQuery = function(query, params)
{
return new Promise(function(resolve, reject)
{
mysql_connection.query(query, params, function(error, results, fields)
{
mysql_connection.end((err) => {
if (err) {
console.log('error terminating connection: ', mysql_connection.threadId);
console.log('error ', err);
}
});

if (error) {
reject(error);
}
resolve({resultSet: results, fieldSet: fields});
});

});
}
}

module.exports = Users;

这是调用模型的脚本

//main.js
let Users = require ('./models/user');

function getUser1Info()
{
user1.getUserByEmail('original.logger@example.com')
.then(userData1 => {
let ctime = new Date();
console.log(ctime.getTime(), 'user data: ', userData1.user_id)
})
.catch((error) => {
console.log('error!', error);
});
}


function getUser2Info()
{
user2.getUserByEmail('billy.b.parker@example.com')
.then(userData2 => {
let ctime = new Date();
console.log(ctime.getTime(), 'user data: ', userData2.user_id)
})
.catch((error) => {
console.log('error!', error);
});
}

let user1 = new Users();
let user2 = new Users();

setTimeout(() => {
getUser1Info();
}, 1000);

setTimeout(() => {
getUser2Info();
}, 999);

此代码未从我的 API 中的 models/user.js 中修改。这让我相信我在 Express 中创建模型的方式从根本上被破坏了。

感谢任何帮助

最佳答案

If you using the node-mysql module, just remove the .connect and .end. Just solved the problem myself. Apparently they pushed in unnecessary code in their last iteration that is also bugged. You don't need to connect if you have already ran the createConnection call

Cannot enqueue Handshake after invoking quit

我还会做什么:使用 Knex 而不是普通的 mysql

关于mysql - Node MySQL 错误 - 错误 : Cannot enqueue Quit after invoking quit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48333749/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com