javascript - JavaScript + Discord.js:另一个对象内的对象返回TypeError:无法读取未定义的属性
问题描述
所以我正在为 Discord 构建一个 RPG 机器人,我正在制作装备系统(你知道,剑、盔甲)。我遇到了一个错误,所有其他对象都可以工作,但是当我将它记录到控制台时userData
,只有设备对象返回。当用户运行时,undefined
我尝试过,但它返回了。这是我的代码:Object.assign(userData[message.author.id].equipment, {equipmentData})
!setup
TypeError: 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 中的其他对象。
解决方案
推荐阅读
- generics - 返回 impl trait + 'static 的泛型函数的生命周期要求
- python - Pandas read_excel 返回 PendingDeprecationWarning
- python - Pandas 滚动、重采样和应用功能
- mysql - 查询相似短语
- javascript - JavaScript `降低`性能
- .net - Visual Studio 2015,OneDrive - aspx.vb 页面需要从 OneDrive 访问文件
- c# - 使用 MSTest 作为测试框架时是否可以使测试方法通用?
- regex - 正则表达式搜索包含其他短语的短语并且不标记其他任何内容
- reactjs - 如何在 React 中渲染递归组件?
- debian - debuild - 依赖集但没有版本