首页 > 解决方案 > TypeError:无法读取未定义的属性(读取“toJSON”)和另外一个

问题描述

const fs = require('fs');
const { Client, Collection, Intents } = require('discord.js');
const { token } = require('./config.json');

const client = new Client({ intents: [Intents.FLAGS.GUILDS] });

client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.data.name, command);
}


client.once('ready', () => {
    console.log('Ready!');
});

client.on('interactionCreate', async interaction => {
    if (!interaction.isCommand()) return;

    const command = client.commands.get(interaction.commandName);

    if (!command) return;

    try {
        await command.execute(interaction);
    } catch (error) {
        console.error(error);
        return interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
    }
});

client.login(token);

that's the index.js file 


the error= TypeError: Cannot read properties of undefined (reading 'name')
    at Object.<anonymous> (C:\Users\shado\OneDrive\Pulpit\Command\index.js:12:35)
    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





there is also a second issue


TypeError: Cannot read properties of undefined (reading 'toJSON')
    at Object.<anonymous> (C:\Users\shado\OneDrive\Pulpit\Command\deploy-commands.js:11:29)
    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




    const fs = require('fs');
    const { REST } = require('@discordjs/rest');
    const { Routes } = require('discord-api-types/v9');
    const { clientId, guildId, token } = require('./config.json');
    
    const commands = [];
    const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
    
    for (const file of commandFiles) {
        const command = require(`./commands/${file}`);
        commands.push(command.data.toJSON());
    }
    
    const rest = new REST({ version: '9' }).setToken(token);
    
    rest.put(Routes.applicationGuildCommands(clientId, guildId), { body: commands })
        .then(() => console.log('Successfully registered application commands.'))
        .catch(console.error);
    

^ deploy-commands.js 第二个问题涉及到

我不知道发生了什么,它昨天工作,一切都崩溃了,请帮忙

自从上次在我的电脑上运行以来,没有发生重大变化。这两个文件本身的代码完全没有,只是添加了额外的命令

标签: javascript

解决方案


我遇到过同样的问题。尝试打印 command 和 command.data 并查看类型。对我来说,当我执行 typeof(command.data) 时,我看到我的一个对象是未定义的。这是由于命令文件夹中的 user.js 文件中的拼写错误。我写的不是数据,而是数据。


推荐阅读