Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
How to throw an Error inside pg pool query in Express JS?

I am facing trouble when I try to throw an Error (to mark if there are no username) inside pool query. What I am expect is the error was catch by checkUsername and make it standard json response. But, I always get an error like this:

error details

Here is my code in controller:

const checkUsername = (req, res) => {
  const service = new AuthService()
  try {
    service.chekcUsernameIsExist(req.body.username, res)

    return res.status(200).send('Username is exist')
  } catch (error) {
    const response = {
      status: false,
      message: error.message,
      error_code: 400
    }
    return res.status(400).send(response)
  }
}

And here is my code in service:

class AuthService {
  chekcUsernameIsExist (username) {
    const query = `select * from admins where username = '${username}';`

    pool.query(query, (err, results, release) => {
      if (err) {
        throw err
      } else if (results.rowCount !== 0) {
        console.log('Username is exist')
      } else {
        console.log('Username is not exist')
        throw new Error('Username is not exist')
      }
    })
  }
}

I try to add try catch block inside checkUsernameIsExist but still got the same problem. At the stupid, I add a variable to which have value 1, 2, or 3 to mark is username exist, not exist, or other error.

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda