i am building CRUD application i am able to read and delete data but i am unable to update data in the database table for the specific route, also when i tried to update data in the table with id 1 its updating the data in all ids
app.get('/doctor/edit/:id', async (req, res) => {
const id = req.params.id;
//console.log(id);
const data = await doctor.getDoctorById(id);
res.render('editDoctor', { data });
});
//update doctor
app.post('/doctor/edit/:id', async (req, res) => {
const data = req.body;
const id = req.params.id; // Get the 'id' from the URL parameters
console.log(data);
console.log(id);
try {
await doctor.updateDoctor(data, id); // Pass the 'id' to the updateDoctor function
res.redirect('/doctor');
} catch (error) {
console.log('Error updating patient:', error);
res.status(500).send('Internal Server Error');
}
});
// function to update the information
const updateDoctor = async (updatedData) => {
const { id, name, specialization, contact, appointment } = updatedData;
const query = `
UPDATE doctor
SET name = $2, specialization = $3, contact = $4, appointment = $5
WHERE id = $1`;
const values = [id, name, specialization, contact, appointment]; // Correct order of values
await pool.query(query, values);
};
i am using ejs in front end , i am expecting that the existing doctor information should update with this code but i am receiving the data but its not updating in the database.
2
Answers
Try with out backticks for const query and try to add excpeiton handling block for query method execution i.e, (error, results) => {} . Example given below
i.e.,