MySQL 中的 SELECT 不起作用 [英] SELECT in MySQL not functioning
问题描述
有一次我试图将 MySQL 用于我的 Nodejs 项目.无论我做什么,它都不会正确执行 SELECT 操作,或者会出错而无法工作.从那以后我尝试使用 FS,但我意识到开销会太大,Firestore 会太复杂.这是我的代码的快速片段 - 它崩溃说TypeError:无法读取未定义的属性'标志'"(flag在数据库中,ID和Flag都是数据库中的字段)
At one point I was trying to use MySQL for my Nodejs project. No matter what I did it wouldn't perform the SELECT operation correctly or would bug out and not work. I have since tried using FS but I realized the overhead is going to be too much and Firestore is going to be far too complicated. Here is a quick snip of my code - it crashes saying "TypeError: Cannot read property 'flag' of undefined" (flag is in the database, ID and Flag are both fields in the database)
我是否滥用了它,我该如何解决?(如果它继续不写入数据库或正确读取数据库,我可能最终需要更多调试帮助)
Am I misusing this and how can I fix it? (I might end up needing more help with debugging if it continues to not write to the database or read from it properly)
con.query(`SELECT * FROM pedodb WHERE ID = ?`, [msg.author.id],
function (err, result, fields) {
//callback(null, rows, fields);
console.log("Query Initiated")
if (ageCheck(msg.content.toLowerCase(), "8", "15")) {
if (result.flag in {
1: "P",
2: "S",
3: "YA",
4: "A"
}) {
}
}
})
推荐答案
你应该检查 if (err) { console.log(err);返回;}
作为回调函数中的第一行.可能是该表不存在.
You should check if (err) { console.log(err); return; }
as a first line in your callback function. Could be the table doesn't exist.
此外,result 是一个数组,因此,您应该检查 result.length >0
并使用 result[0].flag
,如果表中存在这样的列.
Also, result is an array, so, you should check if result.length > 0
and use result[0].flag
, should such column exist in the table.
这篇关于MySQL 中的 SELECT 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!