'TypeError: Cannot read properties of undefined (reading 'toLowerCase') ( on setup.js )

I am really new on this, i have this code, and when i executed this command on discord, its showing TypeError: Cannot read properties of undefined (reading 'toLowerCase') without showing error log in terminal / console, can anyone help me to fix this ?

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.aliases = exports.category = exports.run = exports.description = exports.name = void 0;
const name = 'setup';
exports.name = name;
const description = 'Setup check bot';
exports.description = description;
const category = "Utility";
exports.category = category;
const aliases = ['s'];
exports.aliases = aliases;
const run = async (client, message, args) => {
    const configcollection = client.db.get('config');
    if (args[0].toLowerCase() == 'delete') {
        configcollection.find({ guildId: message.guild.id }).then(docs => {
            if (docs.length == 0)
                return message.reply('No settings was saved');
            configcollection.remove({ guildId: message.guild.id }).then(e => {
                message.channel.send('Success remove settings');
            }).catch(err => {
                console.error(err);
                message.reply('Failed remove settings');
            });
        });
    }
    else {
        configcollection.find({ guildId: message.guild.id }).then(docs => {
            if (docs.length != 0)
                return message.reply('Settings already exist, please delete current settings');
            if (!message.mentions.roles.first() && !message.mentions.channels.first())
                return message.reply('Please mention channel and role');
            configcollection.insert({ guildId: message.guild.id, channelId: message.mentions.channels.first().id, roleId: message.mentions.roles.first().id }).then(a => {
                message.channel.send('Success saving settings');
            }).catch(err => {
                message.reply('Failed saving settings');
                console.error(err);
            });
        }).catch(err => {
            message.reply('Failed load data');
            console.error(err);
        });
    }
};
exports.run = run;
//# sourceMappingURL=setup.js.map


Solution 1:[1]

Replace your if condition with if (args[0]?.toLowerCase() == 'delete')

In some case, your args[0] is undefined that's why it is throwing the error. Use of ? will only check for toLowerCase() when args[0] is defined.

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 Ritik Banger