Answer the question
In order to leave comments, you need to log in
Try catch or then catch for rest api?
There is an api request handler, where db.query() is a request to PostgreSQL, which returns a Promise with resolve on success, and reject on error (for example, if the database password is wrong).
The request handler itself:
async getAll(req, res) {
const query = `SELECT * FROM users`
return db
.query(query)
.then(({ rows, rowCount }) => res.status(200).send({ rows, rowCount }))
.catch(error => res.status(400).send({ error }))
// try {
// const { rows, rowCount } = await db.query(query)
// return res.status(200).send({ rows, rowCount })
// } catch (error) {
// return res.status(400).send(error)
// }
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question