【问题标题】:MySQL NodeJS Error : Cannot enqueue Query after invoking quit using transactionsMySQL NodeJS错误:使用事务调用退出后无法将查询入队
【发布时间】:2020-03-10 15:35:54
【问题描述】:

我一直在为 NodeJS 和 Restify 实现 MySQL,并且在查询方面一切都运行良好。但是当我想使用通过事务更新数据的功能时,我得到了以下错误:Error: Cannot enqueue Query after invoking quit.

这是踪迹:

(node:10877) UnhandledPromiseRejectionWarning: Error: Cannot enqueue Query after invoking quit.
at Protocol._validateEnqueue (/home/node_modules/mysql/lib/protocol/Protocol.js:215:16)
at Protocol._enqueue (/home/node_modules/mysql/lib/protocol/Protocol.js:138:13)
at Connection.query (/home/node_modules/mysql/lib/Connection.js:201:25)
at Connection.beginTransaction (/home/node_modules/mysql/lib/Connection.js:154:15)
at Promise (/home/app/mysql/repository/ClientDao.ts:56:23)
at new Promise (<anonymous>)
at ClientDao.updateIps (/home/app/mysql/repository/ClientDao.ts:55:16)
at /home/app/routes/ips/put.ts:22:31
at Generator.next (<anonymous>)
at fulfilled (/home/app/routes/ips/put.ts:5:58)

这是代码:

this.conn.connect();
return new Promise<any>((resolve, reject) => {
        this.conn.beginTransaction( (err) => {
            if(err) {
                reject(err);
                return;
            }
            this.conn.query('UPDATE client SET ips_allowed = ? WHERE id = ?', [ips, client.id], (error, results, fields) => {
                if(error) {
                    return this.conn.rollback(() => reject(error));
                }
                this.conn.commit((error) => {
                    if(error) {
                        return this.conn.rollback(() => reject(error));
                    }
                    this.conn.end();
                });
            });

        });
    });

如果有人能帮我找出错误,我将不胜感激。正如我之前所说,它只发生在交易中。

【问题讨论】:

    标签: javascript mysql node.js node-modules restify


    【解决方案1】:

    问题已由我自己解决,如果您正在对另一个人进行先前的查询,请小心,当您执行 .end() 时,mySql.createConnection() 载体对象变为“未定义”或“空”,所以它不能再次连接,直到它再次被创建。如果实现的方式不是最优的,我想这很明显。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-18
      • 2017-06-19
      • 2012-12-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 2020-03-08
      • 2021-12-13
      相关资源
      最近更新 更多