Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

276
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda