skip to Main Content

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


  1. 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.,

    const updateUser = (request, response) => {
      const id = parseInt(request.params.id)
      const { name, email } = request.body
    
      pool.query(
        'UPDATE users SET name = $1, email = $2 WHERE id = $3',
        [name, email, id],
        (error, results) => {
          if (error) {
            console.log("error"+error);
            throw error
          }
          response.status(200).send(`User modified with ID: ${id}`)
        }
      )
    }
    
    Login or Signup to reply.
  2.  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);
            };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search