Search code examples
htmlnode.jsexpressejs

Trying to update a user in my Node.js application, but I'm getting an error. However, the update works correctly when I use Postman


// Controller
exports.updatedTraningUserData = (req, res) => {
    const traningId = req.params.id;
    const updatedUserData = req.body; // The updated user data from the request body

    // Update the user in the database
    const sql = 'UPDATE traning SET certificationCode = ?, fullName = ?, company = ?, position = ?, email = ?, telephone = ?, date = ?, title = ?, futureTopics = ? WHERE id = ?';
    const values = [updatedUserData.certificationCode, updatedUserData.fullName, updatedUserData.company, updatedUserData.position, updatedUserData.email, updatedUserData.telephone, updatedUserData.date, updatedUserData.title, updatedUserData.futureTopics, traningId];

    db.query(sql, values, (err, results) => {
      if (err) {
        console.error('Error updating the user: ' + err.message);
        res.status(500).send('Error updating the user');
      } else {
        const traningUpdate = results;
        console.log(traningUpdate);
        console.log('User updated successfully');
        res.status(200).send('User updated successfully');
      }
    });
};

Inside console.log(traningUpdate)

enter image description here

But when I do with postman with same path : http://localhost:3000/traning-user/12
Passing Value with json it works !

enter image description here

Here my traningUser.ejs where is my form :

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>traning User</title>
</head>
<body>
    <h1><%= traning.fullName %></h1>
    <div class="container">
      <form class="is-readonly" action="/traning-user/:id" method="post">
        <div class="form-group">
          <label for="certificationCode">Certification Code</label>
          <input type="text" class="form-control is-disabled" id="certificationCode" placeholder="<%- traning.certificationCode %>" value="<%- traning.certificationCode %>" name="certificationCode" disabled>
        </div>
      <div class="form-group">
          <label for="fullName">Fullname</label>
          <input type="text" class="form-control is-disabled" id="fullName" placeholder="fullName" value="<%- traning.fullName %>" name="fullName" disabled>
        </div>
        <div class="form-group">
          <label for="company">Company</label>
          <input type="text" class="form-control is-disabled" id="company" placeholder="<%- traning.company %>" value="<%- traning.company %>" name="company" disabled>
        </div>
*****ECT...******
</body>
</html>

My route.js :

// Fichier traningRoutes.js dans le répertoire routes
const express = require('express');
const router = express.Router();
const traningController = require('../controllers/traning');

router.post('/traning', traningController.createTraningUser);
router.delete('/traning/:id', traningController.deleteTraningUsers);
router.get('/traning-user', traningController.getTraningUsers);
router.post('/traning-user/:id', traningController.updatedTraningUserData); //HERE
router.get('/display/:id', traningController.test);
router.get('/traning-user/:id', traningController.getTraningUserById);

module.exports = router;

I want to update my database with a post request via my HMTL form


Solution

  • The error is probably due to the line here:

    ...
        <div class="container">
          <form class="is-readonly" action="/traning-user/:id" method="post"> <--this line
            <div class="form-group">
    ...
    

    In the action field you are not binding the id correctly. I believe in ejs, you need to bind the variable like this:

          <form class="is-readonly" action="/traning-user/<%= training.id %>" method="post">