【问题标题】:Cannot read property 'name' of undefined, i need some help here无法读取未定义的属性“名称”,我需要一些帮助
【发布时间】:2021-06-14 20:55:23
【问题描述】:

const fs = require('fs');

function loadCommands(client) {
    fs.readdir('commands/', (err, files) => {        

        if (err) console.log(err);
    
        const jsfile = files.filter(f => f.split('.').pop() === 'js');
        if (jsfile.length <= 0) {
            return console.log('Bot Couldn\'t Find Commands in commands Folder.');
        }
    
        jsfile.forEach((f, i) => {
            const pull = require(`../commands/${f}`);
            client.commands.set(pull.config.name, pull);
            pull.config.aliases.forEach(alias => {
                bot.aliases.set(alias, pull.config.name);
            });
        });
    });
}

module.exports = {
    loadCommands
}

我不知道为什么会出现错误,代码看起来很棒...

错误出现在 client.commands.set(pull.config.name, pull);bot.aliases.set(alias, pull.config.name); 显示未定义。

我需要一些帮助,我正在尝试为我的机器人制作自定义前缀,我真的需要这段代码!

The error in the code

【问题讨论】:

  • 错误告诉你究竟出了什么问题;即pull.configundefined。将文件修复为具有配置键的对象,或检查代码中的未定义。

标签: javascript node.js discord discord.js bots


【解决方案1】:

../commands/whateverFile.js 中的一个文件不导出 config,或导出 config 但未在其上放置 .name 属性。您没有向我们展示任何这些文件,因此很难猜出问题所在。

你可以试试这个来找出哪个文件是坏的。

        jsfile.forEach((f, i) => {
            const pull = require(`../commands/${f}`);
            if (!pull || !pull.config || !pull.config.name) {
                throw new Error (`bogus require file ${f}`)
            }
            client.commands.set(pull.config.name, pull);
            pull.config.aliases.forEach(alias => {
                bot.aliases.set(alias, pull.config.name);
            });
        });

【讨论】:

    猜你喜欢
    • 2019-01-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2022-01-22
    • 2021-11-13
    • 2022-01-14
    • 2022-06-22
    相关资源
    最近更新 更多