node mysql error-error:调用quit后无法排队退出

zfycwa2u  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(407)

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

Error: Cannot enqueue Quit after invoking quit

奇怪的是,我得到了一个有效的和正确的结果返回到api请求,但是,显然是出了问题。
我在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中创建模型的方式已经从根本上被打破了。
感谢您的帮助

ctrmrzij

ctrmrzij1#

如果您使用的是node mysql模块,只需删除.connect和.end。我自己解决了问题。很明显,他们在最后一次迭代中引入了不必要的代码,而这些代码也有缺陷。如果已经运行了createconnection调用,则不需要连接
调用quit后无法将握手排队
我还要做的是:使用knex而不是普通的mysql

相关问题