Discord.js args TypeError:无法读取未定义的属性“toLowerCase”

时间:2021-04-11 12:03:02

标签: node.js discord.js

我正在尝试编写一个将“eu”之类的词转换为“europe”的代码,但我不断收到错误 TypeError:如果用法正确,则无法读取 undefined 的属性 'toLowerCase'

const Discord = require('discord.js')
const db = require('quick.db')
module.exports = {
    name: 'code',
    description: 'sends your among us code to the codes channel',
    example: 'code AAAAAA eu',
    async execute(message, args, client, prefix) {
        let ch = db.fetch(`${message.guild.id}_codechannel`)
        if(ch === null) ch = 'not set'
        if(ch === 'not set') return message.channel.send(`A codes channel has not been set here tell an admin to set it using the config command`)

        const voiceChannel = message.member.voice.channel;
        if(!voiceChannel) return message.channel.send('You must join a voice channel')

        const inv = await voiceChannel.createInvite({
            maxAge: 0,
            unique: false,
            maxUses: 100,
        });

        const thecode = args[0];
        if (thecode.length !== 6) return message.reply('The code can only be 6 characters long');
        const thecodeUC = thecode.toUpperCase();
        const region = args[1];
        let r = '';
        if(region.toLowerCase() === 'eu' || region.toLowerCase() === 'europe') r = 'Europe';
        if(region.toLowerCase() === 'na' || region.toLowerCase() === 'northamerica') r = 'North America';
        if(region.toLowerCase() === 'as' || region.toLowerCase() === 'asia') r = 'Asia';
        if(region === 'undefined' || region === '') return message.channel.send('the only regions available are: Europe(eu), Northamerica(na) and Asia(as)');
        
        let channel = message.guild.channels.cache.get(ch)
        const embed = new Discord.MessageEmbed()
        .setColor('#00ffff')
        .setAuthor(message.member.user.tag, message.author.avatarURL())
        .setTitle('New Among Us Code')
        .addField('Code', `${thecodeUC}`, true)
        .addField('Region', `${r}`, true)
        .addField('Voice Channel', `[Click Here](${inv})`, true)
        .setThumbnail(message.guild.iconURL({ dynamic: true }));
        channel.send(embed);
        message.reply(`Sent your code in the codes channel`);

    }
}

我真的很困惑这是什么问题,因为我有

if(region === 'undefined' || region === '') return message.channel.send('the only regions available are: Europe(eu), Northamerica(na) and Asia(as)');

未定义“区域”时返回消息

1 个答案:

答案 0 :(得分:1)

让我们试着理解它说的是什么: “类型错误:无法读取未定义的属性 'toLowerCase'”

在这里,您尝试读取和调用一个名为 toLowerCase 的函数。

if(region.toLowerCase() === 'eu'

从文档中你可以发现这个函数是字符串数据类型的一部分: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase

这意味着,其他东西,例如undefined没有这个功能

您可能还对“无法读取 property 'toLowerCase' 中的 property 一词感到困惑。嗯,这是因为 String 是一个对象,它定义了属性。函数 toLowerCase 是在对象 String 上定义的属性。

字符串也可以称为数据类型。因此TypeError

这意味着当 JavaScript 解释器读取该行时:

if(region.toLowerCase() === 'eu' || region.toLowerCase() === 'europe') r = 'Europe';

可变区域有一个值未定义

还有支票:

if(region === 'undefined' || region === '') return message.channel.send('the only regions available are: Europe(eu), Northamerica(na) and Asia(as)');

仅在 JS 尝试读取该属性后执行。