Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

118
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda