[英]NodeJS mysql promise returns undefined
I'm trying to do command for discord bot that outputs integer from MySQL table.我正在尝试为从 MySQL 表输出整数的 discord bot 执行命令。
I tried doing this with async/await, with promises, with callbacks but the result is always same.我尝试使用 async/await、promise 和回调来执行此操作,但结果始终相同。 Here I'm trying it again with promises, because in past it somehow worked.
在这里,我用承诺再次尝试,因为在过去它以某种方式起作用。 Now it won't.
现在不会了。
Here's function that returns promise:这是返回承诺的函数:
exports.checkAccess = (user) => {
return new Promise((resolve,reject) => {
sql.condb.query("SELECT `accessLevel` FROM `users` WHERE `DiscordID` = '" + user + "' LIMIT 1", function(err,rows){
if(err) reject(err);
else resolve(rows);
})
});
}
And here's code that assigns result to accessLevel variable:这是将结果分配给 accessLevel 变量的代码:
let accessLevel = -1;
cmds.checkAccess(message.author.id).then(rows=>{
accessLevel = rows[0].accessLevel;
}).catch((err)=>{
console.log(err);
});
Catch function catches error that says "TypeError: Cannot read property 'accessLevel' of undefined". Catch 函数捕获错误,提示“类型错误:无法读取未定义的属性 'accessLevel'”。
It seems that your query does not return any results rather then undefined promise.您的查询似乎没有返回任何结果,而不是未定义的承诺。
I suggest you to put your query into variable and use console.log to get generated query so that you can check your final query.我建议您将查询放入变量并使用 console.log 获取生成的查询,以便您可以检查最终查询。
let query = "SELECT `accessLevel` FROM `users` WHERE `DiscordID` = '" + user + "' LIMIT 1";
console.log(query);
Try to select stdout query into your mysql editor.尝试在 mysql 编辑器中选择 stdout 查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.