Search code examples
javascriptnode.jsdiscorddiscord.js

TypeError: Cannot read properties of undefined (reading 'catch')


I am trying to get channel with an id or mention, the code itself works but when user gives wrong ID it says: TypeError: Cannot read properties of undefined (reading 'catch')

Can anyone help me?

I tried this:

message.guild.channels.cache.find(channel => channel.id == args[0]).catch(err => {});

And this:

message.guild.channels.cache.get(args[0]).catch(err => {});

These both things give me error.

Heres the code:

if (args[0].startsWith("<#")) channel = message.mentions.channels.first();
        else channel = message.guild.channels.cache.get(args[0]).catch(err => {
    //do stuff
    })
    

Solution

  • Okay so i found out the answer with help of @JeremyThille: `When the id is wrong it tries to null.catch()

    I removed catch from the code and added

    if(!channel) //do stuff
    

    Thanks @JeremyThille