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>