'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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
