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

91
Views
How to access URL parameter in the route function and pass it to db query as an integer parameter?

const getContactByContactId = (request, response) => {
    const p_contact_id = parseInt(request.params.contactId);
    pool.query('SELECT api_getContactByContactId($1)', [p_contact_id], (error, results) => {
    if (error) {
      throw error
    }
    response.status(200).json(results.rows[0].api_getcontactbycontactid)
  })
}

Objective: I am creating a PERN-stack web app with a DB table called "contact" to hold contacts. When the user navigates to "http://localhost:3000/contacts/1", I would like to use the code snippet above (housed in react-app/backend/routes/contacts.js) to query the database by passing the URL parameter as an integer to the plpgsql function called "api_getContactByContactId()".

Error: "invalid input syntax for type integer: "NaN" "unnamed portal parameter $1= '...'"

Suspected Problem: It appears from the error and my research that I am not correctly accessing the the URL parameter from "request".

Question: How can I access the URL parameter in the route function and pass it to my db query as an integer? Thank you in advance for any pointers.

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