Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!