Javascript 节点MySQL:从列返回值

Javascript 节点MySQL:从列返回值,javascript,mysql,sql,node.js,Javascript,Mysql,Sql,Node.js,我在节点应用程序Discord bot中使用了mysql。我想从列中选择一个值,但在这样做时遇到困难。这是我的代码: 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

我在节点应用程序Discord bot中使用了
mysql
。我想从列中选择一个值,但在这样做时遇到困难。这是我的代码:

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]
现在,我将第1行中的
前缀设置为
“3”
,它按预期工作(我认为),在我的数据库中打印出值,如下所示:

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

控制台日志事件未触发意味着查询结果集为空。 虽然看起来这不是您正在使用的确切代码,但forEach的参数列表看起来是错误的

    (value => 
        prefix = value.prefix;
        console.log(value.prefix) // doesn't fire
    }
尝试让sql引擎通过sql参数包含变量值,而不是使用字符串插值。我的意思是:

connection.query('SELECT * FROM guilds WHERE guildid = ?',[message.guild.id], function(error, commands) { ...

message.guild.id的值是多少