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

I am trying to query the PostgreSQL database with a post request that simply checks for the existence of a username in a users table, if found, the password should be hashed and compared with the one that was queried. I have tested with postman and I get the error message TypeError: Client was passed a null or undefined query at Client.query. I have tested my connectionString with some other HTTP request (like GET) and it worked, which shows that the connectionString is not the problem. Find below a snippet of my code:

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);
    }
});

The error specified this route/callback as the problem. Any suggestions?

1

1 Answer

I found the issue. It is a mistake on my part. The rows is missing in the if statement.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross

Alexander Ross

Gaming, Esports & Interactive Media Writer

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.

Share this article
Twitter Facebook Pinterest