UnhandledPromiseRejectionWarning для pg.Pool обещание цепочки - PullRequest
0 голосов
/ 20 апреля 2019

Мое обещание возвращает ожидаемый запрос к базе данных из первого оператора .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.

EDIT Основываясь на предложении Криса, я смог получить обещание, вернув 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 Ответ

1 голос
/ 20 апреля 2019

Я бы сделал это примерно так:

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();
});
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...