[英]Discord.js TypeError: Cannot read property 'setActivity' of null
[英]Discord.JS | TypeError: Cannot read property 'run' of null
嘗試運行此代碼時,我不斷收到錯誤“ TypeError:無法讀取null的屬性'run'”。 程序中的其他所有內容都可以正常運行,直到到達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}之外,其他所有代碼都是一樣的。 嘗試刪除這種現象,因為您不使用promise,它不是[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]);
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.