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

Discord机器人报错Cannot read properties of undefined (reading 'answer')求修复

修复Discord机器人谜语匹配的undefined错误

问题场景

开发Discord机器人命令,功能为获取其他机器人发送的Embed标题,匹配本地谜语数据返回对应答案。运行代码时触发错误:Cannot read properties of undefined (reading 'answer'),将谜语数据移至JSON文件后无报错,但返回答案始终为undefined。

原代码:

const riddle = [
    {
      "riddle": "What has to be broken before you can use it?",
      "answer": "egg"
    },
    {
      "riddle": "I’m tall when I’m young, and I’m short when I’m old. What am I?",
      "answer": "candle"
    },
    {
      "riddle": "What month of the year has 28 days?",
      "answer": "all of them"
    },
    {
      "riddle": "What is full of holes but still holds water?",
      "answer": "sponge"
    },
    {
      "riddle": "What question can you never answer yes to?",
      "answer": "Are you asleep yet?"
    }
]
const id = args[0]
    if (isNaN(id)) return;
const msg = await message.channel.messages.fetch(id);
    if (!msg) return;
const answer = riddle.filter((obj) => obj.riddle === msg.embeds[0].title).answer
const emb = new MessageEmbed()
     .setAuthor(msg.embeds[0].title)
     .setDescription(msg.embeds[0].description)
     .setColor(msg.embeds[0].color)
message.channel.send({content: `__**Answer:**__ ${answer}`, embeds: [emb]})

错误原因

  1. filter()返回类型错误:filter()方法始终返回数组,即使只匹配到一个元素。直接访问数组的.answer属性会得到undefined;当无匹配项时数组为空,访问空数组的.answer就会触发Cannot read properties of undefined错误。
  2. 缺少边界检查:未验证目标消息是否包含Embed,若msg.embeds[0]不存在,访问.title也会导致错误。

修复方案

  1. 用find()替代filter():find()返回第一个匹配的对象,无匹配项时返回undefined,可直接访问其.answer属性。
  2. 增加边界检查:验证消息是否包含Embed、是否匹配到对应谜语,避免运行时错误。
  3. 若使用JSON文件存储谜语,直接通过require()导入即可,逻辑与本地数组一致。

修复后的完整代码:

// 若使用JSON文件,替换为 const riddle = require('./riddles.json');
const riddle = [
    {
      "riddle": "What has to be broken before you can use it?",
      "answer": "egg"
    },
    {
      "riddle": "I’m tall when I’m young, and I’m short when I’m old. What am I?",
      "answer": "candle"
    },
    {
      "riddle": "What month of the year has 28 days?",
      "answer": "all of them"
    },
    {
      "riddle": "What is full of holes but still holds water?",
      "answer": "sponge"
    },
    {
      "riddle": "What question can you never answer yes to?",
      "answer": "Are you asleep yet?"
    }
];

const id = args[0];
if (isNaN(id)) return;

const msg = await message.channel.messages.fetch(id);
if (!msg) return;

// 检查消息是否包含Embed
if (!msg.embeds || msg.embeds.length === 0) {
    return message.channel.send("目标消息中没有Embed内容");
}

const targetRiddle = msg.embeds[0].title;
// 用find()匹配第一个符合条件的谜语对象
const matchedRiddle = riddle.find(obj => obj.riddle === targetRiddle);

if (!matchedRiddle) {
    return message.channel.send("未找到对应的谜语答案");
}

const emb = new MessageEmbed()
     .setAuthor(msg.embeds[0].title)
     .setDescription(msg.embeds[0].description)
     .setColor(msg.embeds[0].color);

message.channel.send({content: `__**Answer:**__ ${matchedRiddle.answer}`, embeds: [emb]});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 11:25:27