Reputation: 90
I'm trying to connect my Discord Bot to an MYSQL Database. My schema is:
> CREATE DATABASE my_db;
CREATE TABLE Guilds (
guildId VARCHAR(100) NOT NULL PRIMARY KEY,
guildOwnerId VARCHAR(100) NOT NULL
);
CREATE TABLE GuildConfigurable (
guildId VARCHAR(100) NOT NULL PRIMARY KEY,
cmdPrefix VARCHAR(10) DEFAULT '<',
radioChannelId VARCHAR(100) DEFAULT ' ' NOT NULL,
memberCountId VARCHAR(100) DEFAULT ' ' NOT NULL,
AutoStartRadio VARCHAR (10) DEFAULT 'N'
);
I can write in the Database using:
client.on('guildCreate', (guild) => {
console.log('[INFO]'.green + ' The Bot has been added to a new Server! (' + guild + ')')
connection.query(
`INSERT INTO GuildConfigurable(guildId) VALUES ('${guild.id}')`
)
connection.query(
`INSERT INTO Guilds VALUES('${guild.id}', '${guild.ownerID}')`
)
});
But when trying to read Data using:
client.guilds.cache.forEach(guild => {
connection.promise().query(
`SELECT cmdPrefix FROM GuildConfigurable WHERE guildId = '${guild.id}'`
).then(result => {
guildCommandPrefix.set(guild.id, result[0][0].cmdPrefix);
}).catch(err => console.log(err));
})
I get this error:
TypeError: Cannot read property 'cmdPrefix' of undefined
at /home/ubuntu/discord-bot-v2/main.js:39:55
at processTicksAndRejections (node:internal/process/task_queues:94:5)
TypeError: Cannot read property 'cmdPrefix' of undefined
at /home/ubuntu/discord-bot-v2/main.js:39:55
at processTicksAndRejections (node:internal/process/task_queues:94:5)
Btw. When adding this line:
console.log(result[0][0])
I get this output:
TextRow { cmdPrefix: '<' }
undefined
TypeError: Cannot read property 'cmdPrefix' of undefined
at /home/ubuntu/discord-bot-v2/main.js:40:55
at processTicksAndRejections (node:internal/process/task_queues:94:5)
TextRow { cmdPrefix: '<' }
undefined
TypeError: Cannot read property 'cmdPrefix' of undefined
at /home/ubuntu/discord-bot-v2/main.js:40:55
at processTicksAndRejections (node:internal/process/task_queues:94:5)
But I want to write the result (the value of "cmdPrefix") into a variable to use later. What am I doing wrong?
Upvotes: 0
Views: 1912
Reputation: 1226
It seems like you are performing operations on guilds that aren't in the database.
You are only inserting into the database when the client joins a guild, but are fetching the command prefix from all the guilds the client is in. There must be a guild that your bot is in that is not in the database.
Even though it is displaying cmdPrefix: '<'
inside the TextRow, the fact that it is returning undefined means that the record does not exist.
I would recommend you change console.log(result[0][0])
to console.log(guild.id, result[0][0])
. This will print the guild it is trying to fetch along with the TextRow.
Double-check that every guild id it tries to fetch is, in fact, in the database.
If it is the case that you are missing some guilds, you could run another insert upon a undefined entry, or just use some conditional logic and your problem should be solved.
guildCommandPrefix.set(guild.id, result[0][0].cmdPrefix || '<');
Upvotes: 1