简体   繁体   English

Javascript 中的 UnhandledPromiseRejectionWarning 错误

[英]UnhandledPromiseRejectionWarning error in Javascript

My code is returning some error我的代码返回了一些错误

UnhandledPromiseRejectionWarning: Error: 404 Not Found at _response.transport.request.then (/app/node_modules/snekfetch/src/index.js:193:21) at process._tickCallback (internal/process/next_tick.js:68:7) (node:23) UnhandledPromiseRejectionWarning: Unhandled promise rejection. UnhandledPromiseRejectionWarning: E​​rror: 404 Not Found at _response.transport.request.then (/app/node_modules/snekfetch/src/index.js:193:21) at process._tickCallback (internal/process/next_tick.js:68:7) (节点:23) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。 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().这个错误要么是因为在没有 catch 块的情况下抛出了异步函数,要么是因为拒绝了一个没有用 .catch() 处理过的承诺。 (rejection id: 8) (拒绝编号:8)

Here's my node.js code:这是我的 node.js 代码:

  console.log('Turned on Discord bot');
  bot.user.setActivity(`${bot.users.size} comrades!`, { type: 'WATCHING' });
  bot.channels.get(bot.channels.get(`${mainChatChannelID}`).id).send(`**Resuming processes!** :wave:`)
})

bot.on('message', async message => {

  const args = message.content.split(/[ ]+/)
  const promoLogs = bot.channels.get(`${xpAuditLogChannelID}`)
  const officerRole = message.guild.roles.find(role => role.name === `${officerRoleE}`);
  const groupFunction = await bloxyClient.getGroup(groupID)

  if (message.content.toLowerCase().startsWith(`${prefix}${xpName}`)){
    if (!message.member.roles.exists("name", `${officerRoleE}`)){
      return message.channel.send(`Sorry ${message.author}, but only users with the **\`${officerRoleE}\`** can run that command!`).then(message => message.delete(5000));
    }
    if (!args[1]){
      return message.channel.send(`Sorry ${message.author}, but you're missing the first argument--add or remove?\n**Adding ${xpName}: \`${prefix}${xpName} add 1 username1, username2, username3...\`\nRemoving ${xpName}: \`${prefix}${xpName} remove 1 username1, username2, username3...\`**`).then(message => message.delete(5000));
    }else if (args[1].toLowerCase() !== "add" && args[1].toLowerCase() !== "remove"){
      return message.channel.send(`Sorry ${message.author}, but you didn't provide me with a correct first argument--add or remove?\n**Adding ${xpName}: \`${prefix}${xpName} add 1 username1, username2, username3...\`\nRemoving ${xpName}: \`${prefix}${xpName} remove 1 username1, username2, username3...\`**`).then(message => message.delete(5000));
    }else{
      if (!args[2]){
        return message.channel.send(`Sorry ${message.author}, but you're missing the second argument--number of ${xpName}?\n**Adding ${xpName}: \`${prefix}${xpName} add 1 username1, username2, username3...\`\nRemoving ${xpName}: \`${prefix}${xpName} remove 1 username1, username2, username3...\`**`).then(message => message.delete(5000));
      }else if (isNaN(Number(args[2]))){
        return message.channel.send(`Sorry ${message.author}, but you didn't provide me with a real number.\n**Adding ${xpName}: \`${prefix}${xpName} add 1 username1, username2, username3...\`\nRemoving ${xpName}: \`${prefix}${xpName} remove 1 username1, username2, username3...\`**`).then(message => message.delete(5000));
      }else if (args[2] < 0){
        return message.channel.send(`Sorry ${message.author}, but you need to provide me with a positive number.\n**Adding ${xpName}: \`${prefix}${xpName} add 1 username1, username2, username3...\`\nRemoving ${xpName}: \`${prefix}${xpName} remove 1 username1, username2, username3...\`**`).then(message => message.delete(5000));
      }else if (args[2] > maxXP){
        return message.channel.send(`Sorry ${message.author}, but you need to provide mw with a number that's less than the max ${xpName}--currently set at ${maxXP} ${xpName}.\n**Adding ${xpName}: \`${prefix}${xpName} add 1 username1, username2, username3...\`\nRemoving ${xpName}: \`${prefix}${xpName} remove 1 username1, username2, username3...\`**`).then(message => message.delete(5000));
      }else if (!args[3]){
        return message.channel.send(`Sorry ${message.author}, but you're missing the third argument--the usernames!\n**Adding ${xpName}: \`${prefix}${xpName} add 1 username1, username2, username3...\`\nRemoving ${xpName}: \`${prefix}${xpName} remove 1 username1, username2, username3...\`**`).then(message => message.delete(5000));
      }else{
        if (args[1].toLowerCase() === "add"){
          var userArray = message.content.slice(message.content.indexOf(message.content.split(" ")[3])).split(', ');
          for (i = 0; i < userArray.length; i++){
            var { body } = await snekfetch.get(`https://api.roblox.com/users/get-by-username?username=${userArray[i]}`);
            if (body.success === false){
              var errorEmbed = new Discord.RichEmbed()
                .setColor(0xff4040)
                .setDescription(`:warning: **${userArray[i]} doesn't exist on ROBLOX** :warning:`);
                await message.channel.send(errorEmbed);
            }else{
              var userID = await rbx.getIdFromUsername(`${userArray[i]}`);
              var { body } = await snekfetch.get(`${fireBaseURL}/xpData/users/${userID}.json`);
              var currentXP;

(sorry it's too long, I can't put it here) (抱歉太长了,不能放在这里)

https://pastebin.com/4Q8ShEvJ https://pastebin.com/4Q8ShEvJ

throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch()在没有 catch 块的情况下抛出异步函数,或者通过拒绝未使用 .catch() 处理的承诺

I think it says it all, using async await is good but you have to try catch the block, Maybe you could try this:我认为它说明了一切,使用 async await 很好,但你必须尝试捕获块,也许你可以试试这个:

try{    
  const groupFunction = await bloxyClient.getGroup(groupID)
  //with the following of your code or encapsulate the rest in a function.
}catch (err){
  return console.log(err)
}

maybe for it to work as is :也许让它按原样工作:

let groupFunction;
try{    
  groupFunction = await bloxyClient.getGroup(groupID)
  //with the following of you code or encapsulate the rest in a function.
}catch (err){
  return console.log(err)
}

All your "await" have to be try/catched你所有的“等待”都必须尝试/抓住

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM