node.js - NodeJS-类型错误 : Cannot read property 'name' of undefined

标签 node.js discord.js

我的代码中出现以下错误:如果您能帮助我,那就太棒了!我正在使用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/

相关文章:

post - 为什么我的 ExpressJS 服务器不接受 POST 请求?

node.js - 事件.js :180 throw er;//Unhandled 'error' event ^

node.js - Strongloop 在 afterRemote 方法中创建相关模型对象

javascript - 如何在discord.js 中使用用户ID 来ping 用户?

javascript - Discord.js guildMemberUpdate 事件

node.js - 如何判断发送的消息是否是作者的第一条消息?

node.js - 引导微服务时获取 ConfigService 的正确方法

javascript - 在 setInterval 运行时使用 clearInterval

javascript - 从机器人发送的随机报价 - 意外的标识符

javascript - sequelize 多对多 setter 不存在