I have a Node.js application which used a database that consists of many students and their current marks. I am able to view this in a table by first getting the data from MongoDB and sending it to the .ejs file.
index.js
router.get("/dashboard", function (req, res) {
markModel.find({}, function (err, allDetails) {
if (err) {
console.log(err);
} else {
res.render("marks", { details: allDetails});
}
});
});
marks.ejs
<% if(details!=null) { %>
<table>
<tr>
<th>Student</th>
<th>Mark</th>
</tr>
<% details.forEach(function(item){ %>
<tr>
<td><%= item.name%></td>
<td><%= item.mark%></td>
</tr>
<% }) %>
</table>
What I want to do is create an Update button in every row that will take me to a separate page where I can update a student's mark. I thought of adding a button where I can use the _id
to identify the record but I'm not sure how to pass this value into a GET
request to the page where I can update the marks separately.
<td><a href="/update" class="btn btn-secondary" id = <%= item._id%> >Update</a></td>
Another approach I had was using a form.
<form method="post" action="/marks">
<input type="submit" id="<%= item._id%>" name="<%= item._id%>" value="Update">
</form>
But I got stuck on how to how to pass the value of _id
while redirecting to /update
.
router.post("/dashboard", function (req, res) {
res.redirect("/update")
});
This is what I have so far. I'm not sure what to give for the reqID so I have left it blank.
router.get("/update", (req, res) =>
res.render("update", {
reqID:
})
);
Please let me know if there's anything else I can add and how to resolve this issue.