Reputation: 97
I'm trying to make a code which converts words like "eu" to "europe" but i keep getting the error TypeError: Cannot read property 'toLowerCase' of undefined if the usage was correct
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`);
}
}
I'm really confused what's the issue since i have
if(region === 'undefined' || region === '') return message.channel.send('the only regions available are: Europe(eu), Northamerica(na) and Asia(as)');
to return a message when "region" is undefined
Upvotes: 0
Views: 1388
Reputation: 301
Let's try to understand what it says: "TypeError: Cannot read property 'toLowerCase' of undefined"
In here you are trying to read and call a function called toLowerCase.
if(region.toLowerCase() === 'eu'
From documentation you can find that this function is a part of String data type: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toLowerCase
This means, that other things, for example undefined do not have this function
You might be also confused by the word property in "Cannot read property 'toLowerCase'. Well, this is because String is a object, and it defines properties. Function toLowerCase is a property defined on object String.
String is also what you would call a data type. Hence the TypeError.
This means that when JavaScript interpreter reads the line:
if(region.toLowerCase() === 'eu' || region.toLowerCase() === 'europe') r = 'Europe';
The variable region has a value undefined
And the check:
if(region === 'undefined' || region === '') return message.channel.send('the only regions available are: Europe(eu), Northamerica(na) and Asia(as)');
is performed only after JS trying to read that property.
Upvotes: 1