Discord.js 无法使用 mySQL 在数组中找到项目时出错



我收到此错误:

C:UsersredactedDownloadsredactednode_modulesmysqllibprotocolParser.js:437
throw err; // Rethrow non-MySQL errors
^ 
TypeError: Cannot read property 'reason' of undefined
at Query.con.query (C:UsersredactedDownloadsredactedbot.js:83:30)

这是我的代码:

const args = message.content.split(' ').slice(1);
let member = args.slice(0, 1).join(' ');
con.query(`SELECT * FROM bans WHERE id=${member}`, (err, rows) => {
if (err) throw err;
// console.log("check here "+member);
let reason;
reason = rows[0].reason;
if(reason="undefined") return message.reply(`I did not find any users with that ID banned. You must check by user ID!`);
if(reason) return message.reply(`I found the user banned for the reason **${reason}**`);

我在这里很迷茫。基本上,我需要它在数据库中查找项目。它将名为"id"的列与带有命令 +check 的给定列进行比较(此处为 id(。然后,如果找到一个,它会在 id 的同一行上拉取"原因"。问题是,当运行 +check 但找不到与给定 ID 匹配的 ID 时,它会使 Discord 机器人崩溃并给出该错误。我正在抛出错误(我认为(。我对mySQL相当陌生,如果这只是一个简单的错误,我很抱歉。提前谢谢。

您需要处理未找到行的情况:

const row = rows[0];
if (row) {
const { reason } = row;
// rest of your code
} else {
// no rows found
}

最新更新