'slashCommands.push(slashCommand.data.toJSON()); | TypeError: Cannot read properties of undefined (reading 'toJSON')

I started the bot as usual, in the handler or other file from the slash command I didn't change anything. anyone know how to group? I need it now, and I can't find anything on the internet. I started doing the bot at 1 p.m., now when I am writing this it is 10:13 p.m. and the problem appeared only around 9:30 p.m. I don't know what to write next but they keep telling me so have a nice day / night: D

const fs = require('fs')
const { REST } = require('@discordjs/rest')
const { Routes } = require('discord-api-types/v9')

const token = process.env['token']; //get the token in .env
const guild = process.env['guild']; //get the guild ID in .env
const application_id = process.env['application_id']; //get the application ID in .env

module.exports = (client) => {

    const slashCommands = []; //make a variable

    fs.readdirSync('./slashCommands/').forEach(dir => {
        const slashCommandFiles = fs.readdirSync(`./slashCommands/${dir}/`).filter(file => file.endsWith('.js'));

        for (const file of slashCommandFiles) {
            const slashCommand =require(`../slashCommands/${dir}/${file}`);
            slashCommands.push(slashCommand.data.toJSON());
            if(slashCommand.data.name) { //if the slash command file has a name
                client.slashCommands.set(slashCommand.data.name, slashCommand)
                console.log(file, '- Success') //check if the file load and log in console
            } else {
                console.log(file, '- Error') //if the file doesn't have command name, log it error in console
            }
        }
    });
    
    const rest = new REST({ version: '9' }).setToken(token);

    (async () => {
        try{
            console.log('Start registering application slash commands...')

            await rest.put(
                guild
                ? Routes.applicationGuildCommands(application_id, guild) //registered the slash command in guild
                : Routes.applicationCommands(application_id), //registered the slash command globally
                {
                    body: slashCommands,
                }
            );

            console.log('Successfully registered application slash commands.')
        } catch (err) {
            console.log(err);
        }
    })();

};


Solution 1:[1]

One of the files located in this directory is missing the data object: require(../slashCommands/${dir}/${file});

A fix that will stop the error, but will also render the file useless would be skipping over the file that is missing data or data.toJSON()

for (const file of slashCommandFiles) {
    const slashCommand = require(`../slashCommands/${dir}/${file}`)
    if (!slashCommand.data || !slashCommand.data.toJSON()) continue
    // Carry on with code after here
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Thomasc-3