我正在尝试使用 post 请求查询 PostgreSQL 数据库,该请求仅检查 users 表
中是否存在 username
,如果找到,密码应该是散列并与被查询的比较。我已经用 postman 进行了测试,我收到错误消息 TypeError: Client was passed a null or undefined query at Client.query
。我已经用其他一些 HTTP 请求(如 GET)测试了我的 connectionString 并且它有效,这表明 connectionString 不是问题所在。在下面找到我的代码片段:
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);
}
});
错误将此路由/回调指定为问题。有什么建议么?
最佳答案
我发现了问题。这是我的错误。 if 语句中缺少 rows
。
if (foundUser.rows.length === 0) {
return res.json({ message: "Invalid Username" });
}
关于node.js - 我得到 - "TypeError: Client was passed a null or undefined query at Client.query",即使我的代码中没有 null 或 undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53896506/