【问题标题】:.catch running every time even if it is successful.catch 每次运行即使成功
【发布时间】:2021-03-26 05:22:16
【问题描述】:

为什么这段代码每次都运行 catch? 禁令有效,消息被发送,它完美地记录到审计日志,但消息总是被捕获,有人可以帮我解释为什么会发生这种情况

getBan.ban({reason: banReason})
.then(() => {
    message.channel.send(banEmbed)
    getBan.send(banDM)
})
.catch(message.reply('Something went wrong. Is the user ID valid?'))

它仍然发送错误消息,说 getBan 不是函数(因为用户 id/tag 无效或不在服务器中)

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    这是因为catch 接受了一个在 promise 被拒绝时调用的函数。您正在调用该函数并将其结果设置为此函数。尽量不要立即调用message.reply('Something went wrong. Is the user ID valid?'),而是将函数作为参数传递:

    .catch(() => message.reply('Something went wrong. Is the user ID valid?'))
    

    【讨论】:

      猜你喜欢
      • 2012-12-15
      • 1970-01-01
      • 1970-01-01
      • 2021-04-04
      • 2019-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多