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

119
Vistas
URL query in express js with postgresql

I am trying to make a query from my database through the url, but the response just returns all my persons.

exports.getPersonByName = async (req, res) => {
const query = req.query.q
try{
    const person = await pool.query('SELECT * FROM person WHERE firstname LIKE $1', [query])
    res.status(200).json(person)
} catch(error) {
    res.status(500).json({ message: error.message })
}

}

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

There are two problems with your implementation.

  1. You are missing the urlencoded middleware in the server.js file (Add it after the app.use(express.json()) line):
app.use(express.urlencoded({ extended: true }));
  1. You have not configured the route for the getPersonByName method in person.route.js. You don't need to specify query parameters in the route itself. Currently, your GET /person route is used by the getAllPersons function, so you have two options:
  • Unify the getAllPersons and getPersonByName functions under the same path GET /person and retrieve the parameter if present.

You will be keeping only the getAllPersons route and functions and handle the q parameter in there

exports.getAllPersons = async (req, res) => {
  const query = req.query.q || '';
  try {
    const person = await pool.query(
      'SELECT * FROM person WHERE firstname LIKE $1',
      [query]
    );
    res.status(200).json(person);
  } catch (error) {
    res.status(500).json({ message: error.message });
  }
};
  • Define a different route for the getPersonByName, for example:
router.get('/byname', controller.getPersonByName)

And send your requests to /person/byname?q=Example (Keeping the getPersonByName and getAllPersons methods as defined)

about 4 years ago · Juan Pablo Isaza Denunciar
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