简体   繁体   中英

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

I keep getting the error "TypeError: Cannot read property 'run' of null" when trying to run this code. Everything else in the program runs fine until it reaches the sql.run part.

(Note: there is other code in the program that handles all of the Discord.js part of things, only the part that is causing an issue is here)

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]);
      });
  });

I remember using this a while back.

I went into my old code and all of it is the same except for the {Promise} where you open the database. Try removing that seeing as you do not use promises and it isn't [better-]sqlite3

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

Turning my comment into a full-fledged answer:

You're not returning a database driver to use, and instead are using it on the SQLite module itself. Try returning a connection handle, and using that for your queries. You'll have to wait for the database connection promise to resolve first, so assuming you have support for async/await, you can do something like this:

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]);
      });
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM