英文:
msql query not returning data even though it exists
问题
我正在尝试创建一个登录表单。我从前端发送的数据如下所示:
const username = loginform.username.value.trim();
const password = loginform.password.value.trim();
console.log(username, password)
Axios.post(apiUrlEndpont, {
username: username,
password: password,
}).then((res) => {
if(res.data.err){
console.log(res.data.err)
} else {
console.log(res)
}
})
我成功获取到了用户名和密码。但是由于某种我无法识别的原因,SQL查询不起作用。
以下是处理程序的代码:
export default async function handler(req, res) {
const dbConnection = await mysql.createConnection({
host: "localhost",
database: "testapp",
user: "root",
password: "",
socketPath: ""
})
if (req.method === 'POST') {
const username = req.body.username
const password = req.body.password
console.log(username, password)
dbConnection.query(`SELECT password FROM accounts WHERE username = '${username}'`)
.then((err, rows, fields) => {
console.log("asd")
if(rows.length!=0){
var password_hash=rows[0]['password'];
const verified = bcrypt.compareSync(password, password_hash);
if(verified){
res.send(rows[0])
res.end()
} else {
res.send({err: "Invalid password"})
res.end()
}
}else{
res.send({err: "Invalid username password"})
res.end()
}
})
}
}
尽管数据库中存在用户名,但rows
会返回未定义(undefined)。
我是Node和MySQL的新手,请耐心等待。
英文:
I am trying to make a login form. I send the data from frontend like seen here:
const username = loginform.username.value.trim();
const password = loginform.password.value.trim();
console.log(username, password)
Axios.post(apiUrlEndpont, {
username: username,
password: password,
}).then((res) => {
if(res.data.err){
console.log(res.data.err)
} else {
console.log(res)
}
})
I get the username and password successfully. But the SQL query won't work for some reason which I don't recognize.
export default async function handler(req, res) {
const dbConnection = await mysql.createConnection({
host: "localhost",
database: "testapp",
user: "root",
password: "",
socketPath: ""
})
if (req.method === 'POST') {
const username = req.body.username
const password = req.body.password
console.log(username, password)
dbConnection.query(`SELECT password FROM accounts WHERE username = '${username}'`)
.then((err, rows, fields) => {
console.log("asd")
if(rows.length!=0){
var password_hash=rows[0]['password'];
const verified = bcrypt.compareSync(password, password_hash);
if(verified){
res.send(rows[0])
res.end()
} else {
res.send({err: "Invalid password"})
res.end()
}
}else{
res.send({err: "Invalid username password"})
res.end()
}
})
}
}
rows
will return undefined even though the username exists in the database.
I am a newbie with node and mysql so please be patient
答案1
得分: 1
你可以将代码更改为以下内容并尝试:
sequelize.query(`SELECT password FROM accounts WHERE username = '${username}'`)
.then(([rows]) => {
console.log(rows);
...
})
英文:
can you change your code like this and try.
sequelize.query(`SELECT password FROM accounts WHERE username = '${username}'`)
.then(([rows]) => {
console.log(rows);
...
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论