简体   繁体   English

为什么在从 JSON POST 请求中检索到 email 的查询时,Knex (PostgreSQL) 查询总是返回空的 object?

[英]Why does Knex (PostgreSQL) query always return empty object while query by email retrieved from JSON POST request?

I am trying to build my first react app.我正在尝试构建我的第一个反应应用程序。 In the front end, to sign/log in, when a registered user submits his/her email and correct password, in my server side code for response, a query is done by the retrieved email in PostgreSQL database.在前端,要登录/登录,当注册用户提交他/她的 email 和正确的密码时,在我的服务器端响应代码中,通过在 Z399BD1EE587246FAC6F39BEAA98 数据库中检索到的 email 进行查询。

Signin component code:登录组件代码:

const handleSignin = (req, res, db, bcrypt) => {
const { email, password } = req.body;
if (!email || !password) {
  return res.status(400).json('incorrect form submission');
}

/*** There are 2 tables: users & login **/
/*** START (code for debugging) ***/
console.log("email retrieved from req: ", email);
console.log("length of email retrieved from req: ", email.length);
const check = "tithi@gmail.com";
console.log("length of hard-coded email, check: ", check.length);
db.select('name', 'email').from('users')
      .where('email', '=', email)
      .then(user => {
          console.log("query by email retrieved from req: ", user[0]);
      })
db.select('name', 'email').from('users')
      .where('email', '=', check)
      .then(user => {
          console.log("query by email(check): ", user[0]);
      })
/*** END (code for debugging) ***/

/*** You can ignore this part ***/
db.select('email', 'hash').from('login')
  .where('email', '=',  email)
  .then(data => {
    const isValid = bcrypt.compareSync(password, data[0].hash);
    if (isValid) {
      return db.select('*').from('users')
        .where('email', '=', email)
        .then(user => {
          res.json(user[0])
        })
        .catch(err => res.status(400).json('unable to get user'))
    } else {
      res.status(400).json('wrong password')
    }
  })
  .catch(err => res.status(400).json('wrong email'))
}

module.exports = {
  handleSignin: handleSignin
}

I used Knex here.我在这里使用了 Knex。 The query always returns an empty object.该查询始终返回一个空的 object。 I've checked, the user with this email exists in database.我已经检查过,具有此 email 的用户存在于数据库中。 But query by other attributes or even, an email assigned inside the code works.但是通过其他属性甚至查询,代码内部分配的 email 有效。

output: output:

email retrieved from req:  tithi@gamil.com
length of email retrieved from req:  15
length of hard-coded email, check:  15
query by email retrieved from req:  undefined
query by email(check):  { name: 'tithi', email: 'tithi@gmail.com' }

The email is retrieved correctly that is shown in the console output image.控制台 output 图像中显示的 email 已正确检索。 Another problem is, query by email works right when I input these via front end, running on my localhost.另一个问题是,当我通过前端输入这些在我的本地主机上运行时,email 的查询工作正常。 But query always returns empty object while I POST via Postman application or the back end deployed on Heroku.但是当我通过 Postman 应用程序或部署在 Heroku 上的后端发布时,查询总是返回空的 object。

The reason you don't see results when sending the request via Postman is that you're sending the string tithi@gamil.com , which is not contained in your database.通过 Postman 发送请求时看不到结果的原因是您正在发送字符串tithi@gamil.com ,该字符串不包含在您的数据库中。 Fix the typo and you should see results.修正错字,您应该会看到结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM