Discord.JS | TypeError: Cannot read property 'run' of null

Discord.JS | TypeError: Cannot read property 'run' of null

我在尝试 运行 这段代码时一直收到错误 "TypeError: Cannot read property 'run' of null"。程序 运行 中的其他所有内容都很好,直到它到达 sql.run 部分。

(注意:程序中还有其他代码处理所有 Discord.js 部分,这里只有导致问题的部分)

const Discord = require("discord.js");
const sql = require("sqlite");
sql.open("./warnings.sqlite", {Promise});

   sql.get(`SELECT * FROM warnings WHERE userId ="${message.author.id}"`).then(row => {
    if (!row) {
        sql.run("INSERT INTO warnings (userId, level) VALUES (?, ?)", [message.author.id, 1]);
      console.log(row.level);
    } else {
        sql.run(`UPDATE warnings SET level = ${row.level + 1} WHERE userId = ${message.author.id}`);
      console.log(row.level);
    }
  }).catch(() => {
    sql.run("CREATE TABLE IF NOT EXISTS warnings (userId TEXT, level INTEGER)").then(() => {
        sql.run("INSERT INTO warnings (userId, level) VALUES (?, ?)", [message.author.id, 1]);
      });
  });

我记得以前用过这个。

我查看了我的旧代码,除了打开数据库的 {Promise} 之外,所有代码都是一样的。尝试删除它,因为您不使用 promises 而且它不是 [better-]sqlite3

sql.open("./warnings.sqlite")

将我的评论变成完整的答案:

您没有返回要使用的数据库驱动程序,而是在 SQLite 模块本身上使用它。尝试返回一个连接句柄,并将其用于您的查询。你必须等待数据库连接承诺首先解决,所以假设你支持 async/await,你可以这样做:

const Discord = require("discord.js");
const sql = require("sqlite");
const dbProm = sql.open("./warnings.sqlite", {Promise});
const db = await dbProm;

   db.get(`SELECT * FROM warnings WHERE userId ="${message.author.id}"`).then(row => {
    if (!row) {
        db.run("INSERT INTO warnings (userId, level) VALUES (?, ?)", [message.author.id, 1]);
      console.log(row.level);
    } else {
        db.run(`UPDATE warnings SET level = ${row.level + 1} WHERE userId = ${message.author.id}`);
      console.log(row.level);
    }
  }).catch(() => {
    db.run("CREATE TABLE IF NOT EXISTS warnings (userId TEXT, level INTEGER)").then(() => {
        db.run("INSERT INTO warnings (userId, level) VALUES (?, ?)", [message.author.id, 1]);
      });
  });