我的代码中出现以下错误:如果您能帮助我,那就太棒了!我正在使用discord.js!
<小时/><小时/>TypeError: Cannot read property 'name' of undefined at files.forEach.file (/root/eternity-bot/eternity-bot/index.js:21:33) at Array.forEach () at fs.readdir (/root/eternity-bot/eternity-bot/index.js:18:9) at FSReqWrap.oncomplete (fs.js:135:15)
fs.readdir("./commands/", (err, files) => {
if (err) return console.error(err);
files.forEach(file => {
if (!file.endsWith(".js")) return;
let props = require(`./commands/${file}`);
console.log(`Loading Command: ${props.help.name}.`);
bot.commands.set(props.help.name, props);
props.conf.aliases.forEach(alias => {
bot.aliases.set(alias, props.help.name);
})
});
});
最佳答案
类型错误: A TypeError当传递给函数的操作数或参数与该运算符或函数期望的类型不兼容时抛出该错误。
可能的原因是您的props
未正确加载,并且不包含任何属性help
,从而访问了未知属性的属性name
help
抛出类型错误。类似于以下内容:
let obj = {
o1: {
a: 'abc'
}
};
obj.o1 // gives {a: 'abc'}, as o1 is property obj which is an object.
obj.o1.a // gives 'abc', as a is property of o1, which is property of obj.
obj.o2 // undefined, as there's no o2 property in obj.
obj.o2.a // TypeError as there's no o2 property of obj and thus accessing property a of undefined gives error.
关于node.js - NodeJS-类型错误 : Cannot read property 'name' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53473858/