pg.Pool承诺链的UnhandledPromiseRejectionWarning

时间:2019-04-20 16:58:50

标签: javascript postgresql promise

我的诺言从第一个.then语句返回期望的数据库查询,但是链中的第二个.then失败,并显示UnhandledPromiseRejectionWarning: ReferenceError: client is not defined。据我所知,client已定义,每个承诺都有一个有效的.catch。我在这里做什么错了?

const pool = new pg.Pool(config.db);

pool.connect()
  .then(client => {
    return client.query('SELECT * FROM staffers WHERE id=800')
      .then(res => {
        client.release();
        console.log(res.rows[0]);
      })
      .catch(err => console.error('Error executing first psql query', err.stack));
  })
  .then(client => {
    return client.query('UPDATE staffers SET num_times_tweeted=num_times_tweeted + 1 WHERE id=800')
      .then(res => {
        client.release();
        console.log(res);
      })
      .catch(err => console.error('Error executing second psql query', err.stack));
  })
  .catch(err => {
  client.release();
  console.error('Error acquiring client: ', err.stack);
});

// pool shutdown
pool.end();

我得到的具体错误如下:

(node:44233) UnhandledPromiseRejectionWarning: ReferenceError: client is not defined
    at pool.connect.then.then.catch.err (/Users/danielmiller/.../.../app.js:...)
    at process._tickCallback (internal/process/next_tick.js:68:7)
(node:44233) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:44233) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

编辑 根据克里斯的建议,我能够通过从每个client语句返回.then来实现诺言。我不明白为什么这行得通?同样在client.release();中调用finally似乎并不释放client吗?

pool.connect()

    .then(client => {
        return client.query('SELECT * FROM staffers WHERE id=800')
            .then(res => {
                console.log(res.rows[0]);
                return client;
            })
            .catch(err => console.error('Error executing first psql query', err.stack));
    })

    .then(client => {
        return client.query('UPDATE staffers SET num_times_tweeted=num_times_tweeted + 1 WHERE id=800')
            .then(res => {
                console.log(res);
                return client;
            })
            .catch(err => console.error('Error executing second psql query', err.stack));
    })
    .catch(err => {
        client.release();
        console.error('Error acquiring client: ', err.stack);
    })
    .finally(() => {
        client.release();
        // pool shutdown
        pool.end();
    });

1 个答案:

答案 0 :(得分:1)

我会这样做:

const pool = new pg.Pool(config.db);

let pgClient = null;

pool.connect()
.then(client => {
    // Assign client so we don't need to pass it around
    pgClient = client;
    return pgClient.query('SELECT * FROM staffers WHERE id=800')
    .then(res => {
        // Note, no .release() because we still want to use the client,
        // and that's now handled in the finally block.
        console.log(res.rows[0]);
    })
    .catch(err => console.error('Error executing first psql query', err.stack));
})
.then(() => {
    return pgClient.query('UPDATE staffers SET num_times_tweeted=num_times_tweeted + 1 WHERE id=800')
    .then(res => {
        console.log(res);
    })
    .catch(err => console.error('Error executing second psql query', err.stack));
})
.catch(err => {
    console.error('Error acquiring client: ', err.stack);
})
.finally(() => {
    // Release client to pool
    if (pgClient) {
        pgClient.release();
    }
    // pool shutdown
    pool.end();
});