Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

268
Vistas
How to update a data from mongoose using EJS template?

I found a solution on the internet, but it must switch or create another ejs file. The problem is I'm using modal bootstrap for my edit form. I want to get the id and then update it. I thought of putting a hidden id, but I couldn't do it.

Here's my code.

app.js

app.get("/home", function (req, res) {
  
  User.find({}, function(err, allUser){
   
          res.render('home',{allUser});
        });
});

app.get("/home/:id", function (req, res){
      res.render("home");
    })
    
    
    // EDIT USER
    app.post("/home", function (req, res){
    console.log(req.params.id);
      User.updateOne({ id: req.params.id}, {$set: {
        firstname: req.body.firstname,
        lastname: req.body.lastname,
        email: req.body.username,
        password: req.body.password,
        conpassword: req.body.conpassword,
        accountrole: req.body.accountrole,
      }}, function(err, result){
        if (err) {
          console.log(err);
        } else {
         console.log("Post Updated successfully");
         res.redirect('home');
     }
    });
    
    });

ejs file code

  <div class="tableFixHead">
    <table class="table table-hover citizen-table">
      <thead class="table-dark">
          <!-- <th scope="col">#</th> -->
          <th scope="col">Accout Role</th>
         
          <th scope="col">First Name</th>
          <th scope="col">Last Name</th>
          <th scope="col">Email</th>
          <th scope="col"></th>
          <th scope="col"></th>
        </tr>
      </thead>
      <tbody>
      
      <% allUser.forEach(function(users){ %>
        <tr>
          <th scope="row"><%= users.accountrole %></th>
          
          <td><%= users.firstname %></td>
          <td><%= users.lastname %></td>
          <td><%= users.email %></td>
          <td> <a href="" class="ms-trigger" data-toggle="modal"
            data-target="#myModal" value="<%=users._id %>">
            Edit
          </a> </td>
          <td>Delete</td>
        </tr>
        <% }); %>
      </tbody>
 </table>
</div>
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda