javascript - TypeError: Cannot read properties of undefined (reading 'catch') - Stack Overflow

I am trying to get channel with an id or mention, the code itself works but when user gives wrong ID it

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
    })
    

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
    })
    
Share Improve this question asked Dec 12, 2021 at 12:57 T1ranexDevT1ranexDev 571 gold badge2 silver badges7 bronze badges 4
  • Well yes, if the id is wrong, then .find(channel => channel.id == args[0]) returns null. Then you try to chain null.catch(), of course it can't work – Jeremy Thille Commented Dec 12, 2021 at 13:03
  • Yes but how do i fix it? @JeremyThille – T1ranexDev Commented Dec 12, 2021 at 13:05
  • 1 Don't chain .catch() or anything else to something that can be null. Sidenote, Typescript would display a warning .find(...) can be null – Jeremy Thille Commented Dec 12, 2021 at 13:06
  • So i just simply add if(channel == undefined), right? – T1ranexDev Commented Dec 12, 2021 at 13:07
Add a ment  | 

2 Answers 2

Reset to default 2

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

The error TypeError: Cannot read properties of undefined (reading 'catch') means that you are calling catch on undefined.

Basically, it's undefined.catch(). Assuming you are working with promises, this can happen when a method that's being called is not present in the object. e.g


const myObj = {
  fun1: async () => {
    const res = await callingExternalFun();
    if (res.error) {
      throw new Error('my error');
    }
  },
};

myObj.fun1().catch((e) => {console.log(e.message)}); // my error
myObj.fun2().catch((e) => {console.log(e.message)}); // TypeError: Cannot read properties of undefined (reading 'catch')

Here, myObj has async function fun1 that might return an error. So the .catch here es from Promise.prototype.catch()

However, myObj doesn't have a function fun2, it's undefiend and catch will return this error.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744230129a4564205.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信