You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord.js v13 TypeError问题:Cannot read properties of undefined (reading '0')

Discord.js v13 交互报错:TypeError: Cannot read properties of undefined (reading '0')

问题场景

基于Discord.js v13开发机器人时,触发交互后抛出TypeError: Cannot read properties of undefined (reading '0'),错误栈指向代码第660行。

报错原因

处理SHOP类型交互的代码分支未被包裹在interaction.isSelectMenu()的判断条件内。当触发的是非下拉菜单类交互(比如按钮交互、命令交互)时,interaction.values属性会是undefined,此时直接访问interaction.values[0]就会触发该错误。

修复方案

将处理SHOP类型的代码逻辑,移入interaction.isSelectMenu()的判断块中,确保只有当下拉菜单交互触发时,才去访问interaction.values:

// 示例修复后的代码结构
if (interaction.isSelectMenu()) {
  const selectedValue = interaction.values[0];
  if (selectedValue === 'SHOP') {
    // 你的SHOP分支处理逻辑
  }
  // 其他下拉菜单选项的处理
}
// 其他类型交互的处理逻辑

或者在访问interaction.values[0]之前,先通过可选链操作符做防护性判断:

// 另一种防护性写法
if (interaction.values?.[0] === 'SHOP') {
  // 处理逻辑
}

内容的提问来源于stack exchange,提问作者IncredibleCodr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 14:41:25