【问题标题】:I get - "TypeError: Client was passed a null or undefined query at Client.query", even though null or undefined is no where in my code我得到 - “TypeError:客户端在 Client.query 处传递了一个空或未定义的查询”,即使我的代码中没有空或未定义的查询
【发布时间】: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


【解决方案1】:

我发现了问题。这是我的一个错误。 if 语句中缺少rows

 if (foundUser.rows.length === 0) {
            return res.json({ message: "Invalid Username" });
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 2016-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多