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?