Friday 2 June 2017

Node MySQL: Return a value from a column

I'm using mysql in a Node application, Discord bot. I want to select a value from a column but am having trouble in doing so. This is my code:

let prefix;

connection.query(`SELECT * FROM guilds WHERE guildid = ${message.guild.id}`, function(error, commands) {
    if (error) throw error;

    if (commands.length) {  // the guild is in my database
        commands.forEach(value => 
            prefix = value.prefix;
            console.log(value.prefix) // doesn't fire
        }
    }
});

message.channel.send(prefix.length) // crashes [Cannot read property length of undefined]

I now set prefix in line 1 to "3"and it works as expected ( I think ), printing out the value in my database, which looks as follows:

| guildname | guildid | ownername | ownerid | prefix |
------------------------------------------------------
| MyServer  | 7373732 | Rusty     | 636363  | !      |



via Rusty

No comments:

Post a Comment