我正在创建基于承诺的API。如果我一次发出一个请求,一切正常,但是,如果两个或更多请求在一秒钟内到达服务器,我会收到以下错误
node multiple_sends.js
奇怪的是,我得到了一个有效且正确的结果返回给API请求,但显然有些问题。
我在gitHub上创建了here。您可以查看它,我已经创建了一个包含2个用户的SQL播种器,因此您只需将查询粘贴到您的数据库中(播种机甚至可以创建一个新的数据库)
项目很小,只需将数据库凭据放在configs / mysql.js文件中,然后运行文件// 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;
您将看到返回正确答案,但节点出错。
我不认为错误发生在mysql模块中。原因是,如果我从模型中获取代码并创建CLI脚本,则不会返回任何错误,mysql不会发出任何警告
//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);
这是调用模型的脚本
String html = getUrl("https://en.wikipedia.org/"+url);
此代码未在我的API中的models / user.js中修改。这让我相信我在Express中创建模型的方式从根本上被打破了。
感谢任何帮助
答案 0 :(得分:0)
如果您使用node-mysql模块,只需删除.connect和.end即可。刚刚解决了这个问题。显然,他们在最后一次迭代中推送了不必要的代码,这也是错误的。如果您已经运行了createConnection调用
,则无需连接
Cannot enqueue Handshake after invoking quit
我还会做什么:使用Knex而不是普通的mysql