node.js - 我得到 - "TypeError: Client was passed a null or undefined query at Client.query",即使我的代码中没有 null 或 undefined

标签 node.js postgresql express

我正在尝试使用 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/

相关文章:

javascript - 将逻辑导出为ex​​press.js 中的函数

javascript - Express JS 路由参数

java - 如何在 Java 的 PostgreSQL 中插入 XML 文档?

regex - PostgreSQL - 正则表达式问题

django - 如何从 bash 测试 postgresql 凭据?

javascript - 我应该在每次使用 ExpressJS/Node 进行更改时重新启动应用程序服务器吗?

sql - 如何一次从三个表中返回行?

javascript - 转换为 ObjectId 失败 : Mean Stack

node.js - 更改默认的 Sails 公用文件夹

javascript - 让 express.js 显示公用文件夹