Setting up try catch for TypeError's

Hello I am trying to setup a try catch for errors caused when the code interaction.reply() is ran. But it seems like the try catch statement isn't being caught. Btw, I purposely ran code which would give an error. I just want this error to be caught because my bot is crashing everytime an error like this happens.
No description
No description
4 Replies
d.js toolkit
d.js toolkit13mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button! - Marked as resolved by OP
greatalts
greataltsOP13mo ago
Stack Trace
C:\Users\paulk\Desktop\AltsGreatDiscordBot\node_modules\@discordjs\builders\dist\index.js:1310
components: this.components.map((component) => component.toJSON())
^

TypeError: Cannot read properties of undefined (reading 'toJSON')
at C:\Users\paulk\Desktop\AltsGreatDiscordBot\node_modules\@discordjs\builders\dist\index.js:1310:64
at Array.map (<anonymous>)
at ActionRowBuilder.toJSON (C:\Users\paulk\Desktop\AltsGreatDiscordBot\node_modules\@discordjs\builders\dist\index.js:1310:35)
at C:\Users\paulk\Desktop\AltsGreatDiscordBot\node_modules\discord.js\src\structures\MessagePayload.js:136:109
at Array.map (<anonymous>)
at MessagePayload.resolveBody (C:\Users\paulk\Desktop\AltsGreatDiscordBot\node_modules\discord.js\src\structures\MessagePayload.js:136:49)
at StringSelectMenuInteraction.update (C:\Users\paulk\Desktop\AltsGreatDiscordBot\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:231:56)
at handleMenuSelect (C:\Users\paulk\Desktop\AltsGreatDiscordBot\src\Interactions\MenuSelects\handleMenuSelect.js:81:41)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v20.7.0
C:\Users\paulk\Desktop\AltsGreatDiscordBot\node_modules\@discordjs\builders\dist\index.js:1310
components: this.components.map((component) => component.toJSON())
^

TypeError: Cannot read properties of undefined (reading 'toJSON')
at C:\Users\paulk\Desktop\AltsGreatDiscordBot\node_modules\@discordjs\builders\dist\index.js:1310:64
at Array.map (<anonymous>)
at ActionRowBuilder.toJSON (C:\Users\paulk\Desktop\AltsGreatDiscordBot\node_modules\@discordjs\builders\dist\index.js:1310:35)
at C:\Users\paulk\Desktop\AltsGreatDiscordBot\node_modules\discord.js\src\structures\MessagePayload.js:136:109
at Array.map (<anonymous>)
at MessagePayload.resolveBody (C:\Users\paulk\Desktop\AltsGreatDiscordBot\node_modules\discord.js\src\structures\MessagePayload.js:136:49)
at StringSelectMenuInteraction.update (C:\Users\paulk\Desktop\AltsGreatDiscordBot\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:231:56)
at handleMenuSelect (C:\Users\paulk\Desktop\AltsGreatDiscordBot\src\Interactions\MenuSelects\handleMenuSelect.js:81:41)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Node.js v20.7.0
Code block
const purchaseEmbed = await PURCHASE_EMBEDS.createPurchaseEmbed({
paymentMethod: paymentMethod
})

const productOptionMenu = await buildProductOptionsMenu();

try{
interaction.update(
{
embeds: [purchaseEmbed],
components: [new ActionRowBuilder().addComponents(
productOptionMenu
)]
}
)
}catch(err){
console.log(err)
console.log("ERERFDSFD")
}
break;
const purchaseEmbed = await PURCHASE_EMBEDS.createPurchaseEmbed({
paymentMethod: paymentMethod
})

const productOptionMenu = await buildProductOptionsMenu();

try{
interaction.update(
{
embeds: [purchaseEmbed],
components: [new ActionRowBuilder().addComponents(
productOptionMenu
)]
}
)
}catch(err){
console.log(err)
console.log("ERERFDSFD")
}
break;
Mark
Mark13mo ago
you need to await the promises in the try block for the catch to work
greatalts
greataltsOP13mo ago
ah oke ty Lemme try
Want results from more Discord servers?
Add your server