【问题标题】:Await is only on async fucntion [duplicate]等待仅在异步功能上[重复]
【发布时间】:2020-01-11 10:18:43
【问题描述】:

我的问题:错误:

SyntaxError: await 仅在异步函数中有效

let ChooseEmbed = new Discord.RichEmbed()
.setAuthor("Choissisez le type de serveur")
.setDescription("**Normal `????` ➞ [`Exemple`](https://imgur.com/upload)\n **Gaming** `????` ➞ [`Exemple`](https://imgur.com/upload)\n ")
message.channel.send(ChooseEmbed).then(msg => { 

  msg.react('????').then(() => msg.react('????'));

  const filter = (reaction, user) => {
    return ['????', '????'].includes(reaction.emoji.name) && user.id === message.author.id;
  };

  msg.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
    .then(collected => {
      const reaction = collected.first();

      if (reaction.emoji.name === '????') {
        //Normal
        message.channel.send(VerifiedEmbed).then(msgg => { 

          msgg.react('✅').then(() => msgg.react('❌'));

          const filter = (reaction, user) => {
            return ['✅', '❌'].includes(reaction.emoji.name) && user.id === message.author.id;
          };

          msgg.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
            .then(collected => {
              const reaction = collected.first();

              if (reaction.emoji.name === '✅') {
                console.log("C'est bon")

              } else {
                message.channel.send(AnnulEmbed)
                msgg.delete()
                return;
              }
            })

            .catch(collected => {
            return;
            });
        })

        msg.delete()
      } else {
        //Gaming
        message.channel.send(VerifiedEmbed).then(msggg => { 
          msggg.react('✅').then(() => msggg.react('❌'));

          const filter = (reaction, user) => {
            return ['✅', '❌'].includes(reaction.emoji.name) && user.id === message.author.id;
          };

          msggg.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
            .then(collected => {
              const reaction = collected.first();

              if (reaction.emoji.name === '✅') {
                console.log("C'est bon")
                message.guild.channels.deleteAll();
                message.guild.roles.deleteAll();

                message.guild.setName(`Serveur de ${message.author.username}`).then(g => console.log("g")).catch(console.error);
                message.guild.setIcon(message.author.displayAvatarURL).then(g => console.log("g")).catch(console.error);

                let cat = await message.guild.createChannel("IMPORTANT", "category");

                } else {
                msggg.delete()
                message.channel.send(AnnulEmbed)
                return;
              }
            })
            .catch(collected => {
              return;
            });
        msg.delete()
      }
    )}})
    .catch(collected => {
      return;
    });
})

我有错误:SyntaxError: await is only valid in async function 我该如何解决这个问题?

我已经搜索了错误,但我没有看到它们!我尝试了所有我想知道是否有人可以帮助我的方法^^不要犹豫,帮助我解决我的错误

【问题讨论】:

  • 错误信息很清楚:你不能在不是async的函数中使用await

标签: javascript discord discord.js


【解决方案1】:

您使用await 的函数是从这里开始的then 回调:

msggg.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
  .then(collected => {

...其中包含:

let cat = await message.guild.createChannel("IMPORTANT", "category");

不清楚你为什么要这样做,因为代码在随后的任何地方都没有使用cat

可以进行回调async,这样你就可以在那里使用await

msggg.awaitReactions(filter, { max: 1, time: 60000, errors: ['time'] })
  .then(async (collected) => {
// -----^^^^^^^---------^

...但总的来说,我建议不要直接通过.then.catch 回调将promise 与async 函数混合使用。相反,我建议您选择其中一个并始终使用它。

【讨论】:

    猜你喜欢
    • 2021-08-23
    • 2019-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2016-06-28
    相关资源
    最近更新 更多