如何解决如何修复Node.js中未处理的诺言拒绝?
我收到一条错误消息,我不知道如何解决:
未处理的承诺被拒绝。该错误是由于在没有catch块的情况下抛出异步函数而引起的,或者是由于拒绝了未经.catch()处理的诺言而引起的。要在未处理的承诺拒绝时终止节点进程,请使用CLI标志
--unhandled-rejections=strict
(请参见https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。 (拒绝ID:2)
我的代码:
const Discord = require('discord.js');
const client = new Discord.Client();
const fs = require('fs');
client.login('NzUzNzAwMzQ2MDI2Nzg2ODI2.X1qAJQ.K-2nBZOSxz8k0LAnGmNC_EGaKqQ');
(async () => {
client.on('message',async (message) => {
// Voice only works in guilds,if the message does not come from a guild,// we ignore it
if (!message.guild)
return;
if (message.content === '/join') {
// Only try to join the sender's voice channel if they are in one themselves
if (message.member.voice.channel) {
const connection = await message.member.voice.channel.join();
} else {
message.reply('You need to join a voice channel first!');
}
const dispatcher = connection.playFile('C:/Users/vikto/Desktop/FFBot/Audio/gab.mp3');
}
});
throw();
})()
client.login('NzUzNzAwMzQ2MDI2Nzg2ODI2.X1qAJQ.K-2nBZOSxz8k0LAnGmNC_EGaKqQ');
解决方法
您应该将所有代码包含在try捕获中,因为那里的一些语句也会产生被拒绝的承诺。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。