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

Posted

技术标签:

【中文标题】连接到数据库时无法读取属性“运行”的空错误【英文标题】:Cannot read property 'run' of null error when connecting to a database 【发布时间】:2019-01-11 09:35:34 【问题描述】:

我正在做一个带有硬币系统的机器人,当您尝试将其连接到数据库时,您会收到以下错误:

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

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

【问题讨论】:

你能发布你的sqlite初始化代码吗? const sql = require("sqlite"); sql.open("./coin.sqlite"); 【参考方案1】:

需要先打开数据库

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

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

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

【讨论】:

TypeError: db.get 不是函数

以上是关于连接到数据库时无法读取属性“运行”的空错误的主要内容,如果未能解决你的问题,请参考以下文章

在Active Directory中编辑用户:连接到系统的设备无法运行

无法从节点连接读取描述符:在 Windows 操作系统上使用 ChromeDriver Selenium 连接到系统的设备无法运行错误

带有 Blazor InputFile 组件的“无法读取属性 '_blazorFilesById' 的空错误”

无法使用 IntelliJ/Rider 连接到 Microsoft SQL Server

TypeError:无法读取 null 的属性“调度”

如何将我的 docker 连接到远程数据库?