Search code examples
node.jspostgresqlexpresspg

Async pg pool query takes forever to finish


I am currently working on login/register API for my database search engine. I am using [email protected], [email protected], PostgreSQL 12. The problem is that on one of the machines (Ubuntu bionic 18.0.4 LTS) the query for /register route resolves fine - the user is saved in postgres db, but on Fedora 32 the await function takes forever to resolve.

Here is the code:

db.js:


const pool = new Pool({
  host: "localhost",
  user: "postgres",
  password: "postgres",
  port: 5432,
  database: "jimjones"
});

module.exports = pool;

register route in jwtAuth.js:

router.post("/register", validInfo, async (req, res) => {
  const { email, name, password } = req.body;

  try {
    const user = await pool.query("SELECT * FROM users WHERE user_email = $1", [
      email
    ]);

    //doesnt get past first query

    if (user.rows.length > 0) {
      return res.status(401).json("User already exist!");
    }

    const salt = await bcrypt.genSalt(10);
    const bcryptPassword = await bcrypt.hash(password, salt);

    let newUser = await pool.query(
      "INSERT INTO users (user_name, user_email, user_password) VALUES ($1, $2, $3) RETURNING *",
      [name, email, bcryptPassword]
    );

    const jwtToken = jwtGenerator(newUser.rows[0].user_id);

    return res.json({ jwtToken });
  } catch (err) {
    console.error(err.message);
    res.status(500).send("Server error");
  }
});

The query:

const user = await pool.query("SELECT * FROM users WHERE user_email = $1", [
      email
    ]);

The req.body on Fedora 32 is parsed, so it's not a firewall problem regarding the POST request. Using Postman, the program fails on this query (but only on Fedora 32). Both databases have the same tables on both machines. Sql select query in psql returns data on both machines.

Any suggestions on how to fix/debug this? Any help will be greatly appreciated.


Solution

  • Upgrading to [email protected] solved this issue.

    The whole discussion: https://github.com/brianc/node-postgres/issues/2069