M
M
megorka2021-11-06 09:16:09
JavaScript
megorka, 2021-11-06 09:16:09

TypeError: Cannot read properties of undefined (reading 'forEach'), why does it throw an error?

Throws an error when running the code

const Discord = require('discord.js'),
fs = require('fs'),
    config = require('./config.json');
config.cfg.intents = new Discord.Intents(config.cfg.intents);
    
const bot = new Discord.Client(config.cfg);
bot.login(config.token);

require('./events')(bot);

bot.commands = new Discord.Collection();

const commandsFiles = fs.readdirSync('./commands');

for (const file of commandsFiles) {
    const comand = require(`./commands/${file}`);
    comand.names.forEach(element => {
        bot.commands.set(element,comand);
    });
}

console.log(bot.commands);

Mistake
F:\dsbot\main.js:17
    comand.names.forEach(element => {
                 ^

TypeError: Cannot read properties of undefined (reading 'forEach')
    at Object.<anonymous> (F:\dsbot\main.js:17:18)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question