首页 > 解决方案 > JavaScript + Discord.js:另一个对象内的对象返回TypeError:无法读取未定义的属性

问题描述

所以我正在为 Discord 构建一个 RPG 机器人,我正在制作装备系统(你知道,剑、盔甲)。我遇到了一个错误,所有其他对象都可以工作,但是当我将它记录到控制台时userData,只有设备对象返回。当用户运行时,undefined我尝试过,但它返回了。这是我的代码:Object.assign(userData[message.author.id].equipment, {equipmentData})!setupTypeError: Cannot convert undefined or null to object

client.on('message', message => {
    if (!userData[message.author.id]) {
        userData[message.author.id] = {
            equipment: {
                example_weapon: {
                    ID: 0,
                    Attack: 0,
                    Rarity: "somerarity",
                    Owned: 0
                },
                example_armor: {
                    ID: 0,
                    Defense: 0,
                    Rarity: "",
                    Owned: 0
                }
            }
        };
    }
});

注意:我省略了不重要的代码,例如存储在 userData 中的其他对象。

标签: javascriptobjecttypeerrordiscord.js

解决方案


推荐阅读