连接到数据库时无法读取空错误的属性“运行”

时间:2018-08-03 13:15:01

标签: node.js sqlite bots discord.js

我正在使用带硬币系统的机器人,当您尝试将其连接到数据库时,出现此错误:

(node:17008) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'run' of null
    at Promise (C:\Users\Илья\Desktop\JyxoBot\Jyxo\node_modules\sqlite\main.js:219:19)
    at new Promise (<anonymous>)
    at Database.run (C:\Users\Илья\Desktop\JyxoBot\Jyxo\node_modules\sqlite\main.js:218:12)
    at sql.get.then.catch (C:\Users\Илья\Desktop\JyxoBot\Jyxo\index.js:67:7)
    at <anonymous>
    at runMicrotasksCallback (internal/process/next_tick.js:121:5)
    at _combinedTickCallback (internal/process/next_tick.js:131:7)
    at process._tickCallback (internal/process/next_tick.js:180:9)
    at Function.Module.runMain (module.js:695:11)
    at startup (bootstrap_node.js:191:16)
(node:17008) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
(node:17008) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

代码:

sql.get(`SELECT * FROM coins WHERE userId = [message.author.id]`).then(row => {
  if (!row) { // Can't find the row.
    sql.run("INSERT INTO coins (userId, coins) VALUES (?, ?)", [message.author.id, 0]);
  } else {  // Can find the row.
    let curAmt = Math.floor(Math.random() * 5) +0.3 (row.coins + curAmt);
    if (curAmt > row.coins) {
      row.coins = curAmt;
      sql.run(`UPDATE coins SET coins = ${row.coins + curAmt}, WHERE userId = [message.author.id]`);
    }
    sql.run(`UPDATE coins SET coins = ${row.coins + curAmt} WHERE userId = [message.author.id]`);
  }
}).catch(() => {
  console.error; // Log those errors.
  sql.run("CREATE TABLE IF NOT EXISTS coins (userId TEXT, coins INTEGER)").then(() => {
    sql.run("INSERT INTO scores (userId, coins) VALUES (?, ?)", [message.author.id, 0]);
  });
});

但是机器人运行了,我不明白这是什么错误,一切对我来说似乎都是正确的。

1 个答案:

答案 0 :(得分:0)

您需要先打开数据库

const sql = require("sqlite")
const db = sql.open('./coin.sqlite', { Promise });

然后您可以使用db.run/get

来源:Sqlite Discord.js: Cannot read property 'run' of null