-1

good day
尝试向数据库添加数据时抛出错误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);
      });
  },
};

4

1 回答 1

2

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

.then()丛林非常狂野,所以将东西改造成/ asyncawait这变成

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();
  },
};
于 2021-05-18T14:37:03.503 回答