MySQL查询未返回数据,尽管数据存在。

huangapple go评论59阅读模式
英文:

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); 
        ...
})

huangapple
  • 本文由 发表于 2023年1月3日 19:09:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/74992540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定