Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

node.js - TypeError: Cannot read property 'content' of undefined using try catch?

I have tried to add try and catch to my code so it does what I want it to do, however, I get this error:

TypeError: Cannot read property 'content' of undefined

This is my code:

if (!message.guild.me.hasPermission(`MANAGE_CHANNELS`)) return message.reply('I lack the required permissions to run this command. (Required Permissions: ``MANAGE_CHANNELS``)');          
        if(message.member.hasPermission("MANAGE_CHANNELS")){
            const channel = message.channel;
            
            const Discord = require("discord.js");
            let role = message.mentions.roles.first() || message.guild.roles.cache.find(role => role.name === args.join(" "));
            if(!role){
                message.reply("please mention a role.")
            } else{
                message.reply('Are you sure you want to continue this command, it will disallow send permissions from the role you mentioned. Type ``yes`` to continue.')
                let filter = msg => msg.author.id == message.author.id
                message.channel.awaitMessages(filter, {max: 1, time: 20000}).then(collected => {           
  (Error line)> if (collected.first().content.toLowerCase() !== 'yes') return;
                    try {
                        const embed = new Discord.MessageEmbed()
                                    .setTitle("Channel Locked")
                                    .setColor("RED")
                                    .setDescription(`Channel locked to the specified role`)
                    
                                    message.channel.updateOverwrite(role, {
                                        SEND_MESSAGES: false
                                    })
                                    .then(channel => channel.send(embed))
                                    .catch(console.error)
                    }
                    catch(error) {
                        message.reply("Command either timed out or there was an error.")
                    }
                    })   
            }
                
        }

I'm not at all experienced with try and catch so I'm assuming I have done something wrong. Thanks

question from:https://stackoverflow.com/questions/65923314/typeerror-cannot-read-property-content-of-undefined-using-try-catch

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

prob collected.first() does not return anything. try console.log(collected) for debugging.

maybe u dont get a message


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...