JFSdragonfire
JFSdragonfire

Reputation: 21

Type error: cant read property of execute

I dont understand why this is happening because normally the execute error in the console happens because the file name is different from the module.exports name but in this case I literally have no idea what is cause this error (btw I removed choices):

module.exports.run = {
    name: 'scavenge',
    aliases: ['scv', 'scav'],
    description: 'go scavenge for coins',
    async execute(message, args, cmd, client, Discord, profileData){
        const choices = [];
        const choice = choices[Math.floor(Math.random() * choices.length)];
        const randomNumber = Math.floor(Math.random() * 1000) + 1;
        const response = await profileModel.findOneAndUpdate({
            userID: message.author.id,
        }, {
            $inc: {
                Bobux: randomNumber,
            }
        }
        );
        return message.channel.send(`You searched in **${choice}** and found **${randomNumber} Bobux**`);
    }
}

Console:

TypeError: Cannot read property 'execute' of undefined

Upvotes: 0

Views: 52

Answers (3)

AnanthDev
AnanthDev

Reputation: 1808

See you're exporting the command files as a javascript object, and your error means execute property does not exist in the command file object. and it should be module.exports = not module.exports.run =, so the fix would be

module.exports = {
    name: 'scavenge',
    aliases: ['scv', 'scav'],
    description: 'go scavenge for coins',
    execute: async (message, args, cmd, client, Discord, profileData) => { // defining execute property
        // do your command code here
    }
}

Upvotes: 0

Jeffplays2005
Jeffplays2005

Reputation: 320

The problem you're facing is copying and pasting code(most likely), this mainly is caused when you don't fully understand the code.

When your are using a commandfile.execute, you cannot use a module.exports.run

Therefore, you must replace:

module.exports.run = {

with:

module.exports.execute = {

Your index.js file is requiring a .execute while your exporting a .run!

If you need any more help, please comment.

Upvotes: 0

CherryDT
CherryDT

Reputation: 29012

It should be module.exports = and not module.exports.run =:

module.exports = {
    name: 'scavenge',
    aliases: ['scv', 'scav'],
    description: 'go scavenge for coins',
    async execute(message, args, cmd, client, Discord, profileData){
        // ...
    }
}

Upvotes: 1

Related Questions