Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

database table is not updating in postgres when i am using node.js

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

 app.post('/doctor/edit/:id', async (req, res) => {
    const id = req.params.id;
    const updatedData = req.body;
    try {
        await doctor.updateDoctor(id, updatedData);
        res.redirect('/doctor');
    } catch (error) {
        console.log('Error updating doctor:', error);
        res.status(500).send('Internal Server Error');
    }
});
  
const updateDoctor = async (id, updatedData) => {
            const { 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];
            await pool.query(query, values);
        };
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading