微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

UnhandledPromiseRejectionWarning:未处理的承诺拒绝/

如何解决UnhandledPromiseRejectionWarning:未处理的承诺拒绝/

我使用viber网站上的示例,但出现此错误。有什么事吗谢谢!

https://developers.viber.com/blog/2017/05/24/test-your-bots-locally

(node:11512) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block,or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection,use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
<node_internals>/internal/process/warning.js:32
(node:11512) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future,promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
const ngrok = require("./get_public_url");
const ViberBot = require("viber-bot").Bot;

// Creating the bot with access token,name and avatar
const bot = new ViberBot({
  authToken: "token",name: "EchoBot",avatar: "url",});

const http = require("http");
const port = process.env.PORT || 8080;
return ngrok.getPublicUrl().then((publicUrl) => {
  console.log('Set the new webhook to"',publicUrl);
  http
    .createServer(bot.middleware())
    .listen(port,() => bot.setWebhook(publicUrl));
});

解决方法

您需要赶上诺言拒绝,才能了解问题所在:

return ngrok.getPublicUrl().then((publicUrl) => {
  console.log('Set the new webhook to"',publicUrl);
  http
    .createServer(bot.middleware())
    .listen(port,() => bot.setWebhook(publicUrl));
}).catch(e => console.error(e));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。