Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

265
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!