【问题标题】:Async pg pool query takes forever to finish异步 pg 池查询需要永远完成
【发布时间】:2020-07-17 14:00:09
【问题描述】:

我目前正在为我的数据库搜索引擎开发登录/注册 API。我正在使用 express@4.17.1、pg@7.18.2、PostgreSQL 12。问题是在其中一台机器(Ubuntu 仿生 18.0.4 LTS)上,/register 路由的查询解析得很好——用户保存在 postgres db,但在 Fedora 32 上,等待函数需要永远解决。

代码如下:

db.js:


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

module.exports = pool;

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

查询:

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

Fedora 32 上的 req.body 已被解析,所以这不是关于 POST 请求的防火墙问题。 使用 Postman,程序在此查询上失败(但仅在 Fedora 32 上)。两个数据库在两台机器上都有相同的表。 psql中的sql select查询返回两台机器上的数据。

关于如何修复/调试此问题的任何建议?任何帮助将不胜感激。

【问题讨论】:

    标签: node.js postgresql express pg


    【解决方案1】:

    升级到 pg@8.3.0 解决了这个问题。

    整个讨论: https://github.com/brianc/node-postgres/issues/2069

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-16
      • 1970-01-01
      • 1970-01-01
      • 2017-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多