I am working on looping through records in a MySQL table, displaying them in an EJS file, and when one is clicked on, routing to a page to edit the record. However, I am having trouble passing the single Object back to my routes.js file to route it to the edit page. I have had no problem looping through all the records and printing them, so I know the data is there. Here is the part of the EJS file where I am outputting all of the records for user selection:
<body>
<div class="container">
<div class="col-sm-6 col-sm-offset-3">
<h1><span class="fa fa-chess"></span> View Roommate Agreements</h1><br>
<p align="center">Click on a Roommate Agreement below to view the full response and edit if necessary.</p><br>
<% for (var i=0; i < Agreements.length; i++) { %>
<a href="/editAgreement" style="text-decoration: none; color: #333333">
<div class="well" align="center">
<h3>Roommate Agreement for Room #<%= Agreements[i].roomNumber%></h3>
<p><%= Agreements[i].roommate1%></p>
<p><%= Agreements[i].roommate2%></p>
<% var Agreement = Agreements[i]%> //THIS is where I'm trying to declare the object
</div>
</a>
<% } %>
<hr>
<p align="center">
<a href="/agreement" class="btn btn-default btn-sm"><span class="fa fa-arrow-circle-left"></span> Back</a>
<a href="/logout" class="btn btn-default btn-sm"><span class="fa fa-sign-out-alt"></span> Logout</a>
</p>
</div>
</div>
</body>
And here is the Get function to route the object to the edit page. You can see where I'm trying to log the object to the console to make sure I'm getting the right information, but it's currently printing as "undefined".
app.get('/editAgreement', isLoggedIn, function(req, res) {
res.render('editAgreement.ejs', {
Employee: req.user
})
console.log(req.body.Agreement);
})
I'm not sure if the problem is how I'm declaring the object variable in EJS or how I'm trying to use it in the Get function, as I am new to Javascript. Any tips or help would be appreciated, thanks! :)
Agreementsvariable in the template, you need to first pass it in from therendercall, just like how you passed inEmployee.getfunction in client-siderendercall, but I'm still working on getting the data there. Thatconsole.logfunction currently printsundefinedin the console, which it wouldn't if the data was being passed correctly.