无法捕捉UnhandledPromiseRejection警告中的警告 [英] Cannot catch UnhandledPromiseRejectionWarning in promise

查看:235
本文介绍了无法捕捉UnhandledPromiseRejection警告中的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在尝试兑现我的承诺中的UnhandledPromiseRejectionWarning,但由于某种原因,它无法正常工作。它会忽略我的代码,只是将错误输出到控制台。

so I'm trying to catch UnhandledPromiseRejectionWarning in my promise, but for some reason it's not working. It ignores my code and just outputs the error to console.

错误:


UnhandledPromiseRejection警告:未处理的承诺拒绝
(拒绝ID:1):错误:禁止(无法向此
用户发送消息)

UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error: Forbidden (Cannot send messages to this user)

代码:

e.message.author.openDM().then((message) => {
    message.sendMessage(`test`);
}).catch((error) => {
    e.message.channel.sendMessage(error + "test");
});

这是一个使用discordie的Discord机器人。在我看来,以上代码应通过私人消息向消息作者发送 test一词,如果该程序无法通过bot发送,则会在发送消息的渠道中发送错误消息和test。

tl; dr,上面的代码中的catch无效,如果该bot没有执行,我将在控制台中收到上述错误

This is a discord bot, using discordie. In my mind, the above code should send the word "test" to a messages author via private message, if the bot can't, it will send the error and the word test in the channel they sent their message in. However, the second part (inside the catch) doesn't get executed.
tl;dr, The catch in the above code isn't working and I'm getting the above error in console instead if the bot doesn't have permission to dm the user.

推荐答案

您忘记了返回 then 函数中的>语句。
我想 message.sendMessage('test')返回一个诺言

You forgot the return statement inside the then function. I suppose message.sendMessage('test') returns a promise

e.message.author.openDM().then((message) => {
    return message.sendMessage(`test`);
}).catch((error) => {
    e.message.channel.sendMessage(error + "test");
});

这篇关于无法捕捉UnhandledPromiseRejection警告中的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆