admin管理员组文章数量:1400409
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])
returnsnull
. Then you try to chainnull.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
2 Answers
Reset to default 2Okay 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.
本文标签: javascriptTypeError Cannot read properties of undefined (reading 39catch39)Stack Overflow
版权声明:本文标题:javascript - TypeError: Cannot read properties of undefined (reading 'catch') - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744230129a2596289.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论