矿池关闭 mysql2 discord bot

pool closed mysql2 discord bot

美好的一天
尝试向数据库添加数据时,它抛出错误 UnhandledPromiseRejectionWarning: Error: Pool is closed
需要上传消息id到数据库
如果有另一种按顺序发送mysql查询的方法,我准备考虑,但目前我更喜欢这种方法,我只需要了解问题的原因并找到解决方案

const mysql = require("mysql2");
const mysql_cfg = require("../mysql_cfg.json");

module.exports = {
  name: "test",
  execute(message, args) {
    let lider_id = message.author.id;
    var con = mysql.createPool(mysql_cfg).promise();

    message.delete();

    con
      .execute(`SELECT id, date_unblock FROM blocks WHERE id = "${lider_id}"`)
      .then((result) => {
        message.channel.send("test").then((msg) => {
          setTimeout(function () {
            msg.edit("@here").then(function (message) {
              message.react("+");
            });
          }, 1000);

          return con.execute(`INSERT INTO events (id) VALUES ("${msg.id}")`);
        });
      })
      .then(() => {
        con.end();
      })
      .catch((err) => {
        console.log(err);
      });
  },
};

在开始关闭池之前,您没有等待消息编辑等完成执行。

.then() 丛林非常荒凉,因此将东西改成 async/await,这变成了

const mysql = require("mysql2");
const mysql_cfg = require("../mysql_cfg.json");

module.exports = {
  name: "test",
  async execute(message, args) {
    const lider_id = message.author.id;
    const con = mysql.createPool(mysql_cfg).promise();
    message.delete();

    const result = await con.execute(`SELECT id, date_unblock FROM blocks WHERE id = "${lider_id}"`);
    const msg = await message.channel.send("test");

    setTimeout(async function () {
      const message = await msg.edit("@here");
      message.react("+");
    }, 1000);

    await con.execute(`INSERT INTO events (id) VALUES ("${msg.id}")`);
    con.end();
  },
};