【问题标题】:I am getting "await is only valid in async function" error my code is something like this [duplicate]我收到“等待仅在异步函数中有效”错误我的代码是这样的 [重复]
【发布时间】:2021-02-16 00:34:21
【问题描述】:

如果(命令 === clear){ 常量数量 = arg.join(" ");

    if(!amount) return message.reply('please provide an amount of messages for me to delete')

    if(amount > 100) return message.reply(`you cannot clear more than 100 messages at once`)

    if(amount < 1) return message.reply(`you need to delete at least one message`)

    await message.channel.messages.fetch({limit: amount}).then(messages => {
        message.channel.bulkDelete(messages
)});

【问题讨论】:

  • 您必须在函数中缺少 'async' 关键字。可以在没有 await 的情况下编写 async 关键字,但不允许反向。只需在函数定义中添加异步,这应该可以解决。 const handleSubmit = async (e) => { /*其余代码*/}

标签: async-await discord.js pls


【解决方案1】:

不允许使用没有async 声明的await。请将async 关键字添加到您的函数中。那应该可以解决它。

示例:

如果这段代码在消息事件下,那么代码应该是(或在箭头运算符函数中):

client.on('message', async message => {
    //code here
})

如果这段代码在普通函数下:

async function newFunc(){
    //code here
}

请记住在这种情况下会引发上述错误以供将来参考

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-28
    • 2021-01-11
    • 2019-07-15
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 2019-10-19
    • 1970-01-01
    相关资源
    最近更新 更多