Search code examples
javascripttry-catchdiscorderis

How would I correctly .catch this?


I'm trying to catch // this line, but I can't seem to find where to put the catch to correctly catch it.

      mutedList.forEach((i) => {
      if (i.mutedEnd*1000 < new Date()){
        console.log(i)
        db.collection('muted').doc(i.userID).get().then((q) => {
        db.collection('muted').doc(i.userID).update({
          stillMuted: false
        }).then(
          bot.guilds.get(i.guildID).members.get(i.userID).removeRole(i.mutedRole) // this line | here
        ).catch((err) => {
          bot.createMessage('717822384198910042',{ embed: {
            title: 'ERROR',
            description: '```js\n'+err.stack+'\n```',
            color: hex
          }});
      })}
    )}
  })

I've tried where it is currently, and where I put a HERE


Solution

  • You can't .catch(); that line as it is not a promise.

    Try this code, you may be getting errors because you are trying to use methods on something that may be returning undefined so this checks that the guild and member exist before performing any methods on it.

    then(() => {
        const guild = bot.guilds.get(i.guildID);
        if (guild) const member = guild.members.get(i.userID);
        if (member) member.removeRole(i.mutedRole);
    })
    

    The .catch(); you currently have is only for handling rejections on the original promise db.collection('muted').doc(i.userID).update

    I also noticed you're using Discord.js v11, I recommend updating to v12 as v11 will be scrapped by the end of October.