0

I am confused what exactly node-postgres postgres database returned if no records ?

try {
  const {
    rows: [user]
  } = await pool.query(`SELECT * FROM "users" WHERE username = $1 LIMIT 1`, [
    username
  ]);

  console.log('user', user);

  if (!user || typeof username !== 'undefined') return false;

  return await bcrypt.compare(password, user.password);
} catch (e) {
  console.log(e);
}

How to check if there is no user result

0

1 Answer 1

1

For a SELECT query, the pg.Result rows is an empty array [] with rows.length === 0

The rowCount === 0 as well but this can be different in an INSERT/UPDATE query where it becomes the updated rows.

Sign up to request clarification or add additional context in comments.

1 Comment

I tested the returned variable in NodeJs, it was undefined so I checked typeof user === 'undefined BUT for security reasons and Authentication I wanted to know exactly what should I check for .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.