等待使用sqlite的JS文件3
我是Discord Bot开发人员,我将数据库.db文件用作数据库。我在JS中代码,因此我安装了SQLITE3模块。我的“ messageCreate”事件的代码就在那里:
module.exports = {
name: "messageCreate",
once: false,
async execute(client, message) {
let prefix;
await client.db.get(
`SELECT prefix FROM "Guilds" WHERE id = "${message.guild.id}"`,
async (err, row) => {
prefix = row.prefix;
}
);
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const cmdName = args.shift().toLowerCase();
if (cmdName.length == 0) return;
let cmd = client.commands.get(cmdName);
if (cmd) {
cmd.run(client, message, args);
}
},
};
我想将.get()
函数的重新放在prefix
actible中,但是当我尝试运行时代码,它返回未定义,因此我做了console.log()
,并看到代码没有等待结果并继续运行。 因此,问题是我不知道如何等待.get()
函数的结果。我试图放置等待
,但我什么也没做。是否有另一种方法来等待结果,然后创建一个与结果中的变量?
I'm a discord bot developer and I use a database.db file as a database. I code in JS so i installed the sqlite3 module. My code for the 'messageCreate' event is right there:
module.exports = {
name: "messageCreate",
once: false,
async execute(client, message) {
let prefix;
await client.db.get(
`SELECT prefix FROM "Guilds" WHERE id = "${message.guild.id}"`,
async (err, row) => {
prefix = row.prefix;
}
);
if (message.author.bot) return;
if (!message.content.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).trim().split(/ +/g);
const cmdName = args.shift().toLowerCase();
if (cmdName.length == 0) return;
let cmd = client.commands.get(cmdName);
if (cmd) {
cmd.run(client, message, args);
}
},
};
I want to put the resut of the .get()
function in the prefix
variable, but when I try to run the code, it returns undefined, so I did a console.log()
and saw that the code didn't await the result and continued to run.
So the problem is that I don't know how to await the result of the .get()
function. I tried to put an await
, but i didn't do anything. Is there another way to wait the result and then create a variable with the result in it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
Promise
构造函数()。Use
Promise
constructor (mdn).您能检查是否有错误?我推测您在SQL语法中有一个错误,因为您在此处围绕表名的引号:
添加此信息以进行故障排除目的,让我知道它返回了什么:
Could you check if there's an error? I speculate that you have an error in your SQL syntax, since you are putting quotes around the table name here:
Add this for troubleshooting purposes and let me know what it returns: