【发布时间】:2018-12-22 22:56:29
【问题描述】:
我正在尝试使用 post 请求查询 PostgreSQL 数据库,该请求仅检查 users table 中是否存在 username,如果找到,则应对密码进行哈希处理并与查询的密码进行比较。我已经用邮递员进行了测试,我收到了错误消息TypeError: Client was passed a null or undefined query at Client.query。我已经用其他一些 HTTP 请求(如 GET)测试了我的 connectionString 并且它有效,这表明 connectionString 不是问题。在下面找到我的代码的 sn-p:
router.post("/login", async (req, res, next) => {
try {
// try to find the user first
const foundUser = await db.query(
"SELECT * FROM users WHERE username=$1 LIMIT 1"
[req.body.username]
);
if (foundUser.length === 0) {
return res.json({ message: "Invalid Username" });
}
// if the user exists, let's compare their hashed password to a new hash from req.body.password
const hashedPassword = await bcrypt.compare(
req.body.password,
foundUser.rows[0].password
);
// bcrypt.compare returns a boolean to us, if it is false the passwords did not match!
if (hashedPassword === false) {
return res.status(400).json({ message: "Invalid Password" });
}
return res.json(200).json({ message: "Logged In" });
} catch(err) {
return next(err);
}
});
错误将此路由/回调指定为问题。有什么建议么?
【问题讨论】:
-
控制台日志
req.body.name得到什么价值?
标签: node.js postgresql express