admin管理员组文章数量:1420966
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! :)
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! :)
Share Improve this question asked Mar 29, 2018 at 1:36 Blake LewisBlake Lewis 1133 silver badges11 bronze badges 6- Are you trying to create unique links to each agreement? (Which contain a little extra data like the roommates' names for context?) – Stephen Gheysens Commented Mar 29, 2018 at 1:44
-
Blake, in order to access the
Agreements
variable in the template, you need to first pass it in from therender
call, just like how you passed inEmployee
. – Stephen Gheysens Commented Mar 29, 2018 at 1:52 -
Please post your
get
function in client-side – Shimon Brandsdorfer Commented Mar 29, 2018 at 1:54 -
@StephenGheysens Once a specific agreement is clicked on, I want it to redirect to a page where the user can edit it. I know I will need to pass it from the
render
call, but I'm still working on getting the data there. Thatconsole.log
function currently printsundefined
in the console, which it wouldn't if the data was being passed correctly. – Blake Lewis Commented Mar 29, 2018 at 1:56 - @ShimonBrandsdorfer I'm not quite sure what you mean. Can you clarify? – Blake Lewis Commented Mar 29, 2018 at 1:57
2 Answers
Reset to default 5to passe variables to another page using GET
you have to passe them in the url
, with that you will expose you data and that's not safe and a lot of work if you have a lot of information to pass,you should passe only the id
of the agreement to the get
route, fetch the agreement and then render the view, like :
<% for (var i=0; i < Agreements.length; i++) { %>
<a href="/editAgreement/<%= Agreements[i].id %>" style="text ...
and then :
app.get('/editAgreement/:id', isLoggedIn, function(req, res) {
let agreementId = req.params.id;
let agreement = // go fetch the agreement from the database like you did before with the list of agreements
res.render('editAgreement.ejs', agreement)
})
If you want to get the Object sent along with your GET
request, you need to send them along, and it doesn't seem from your code that you are doing it.
The quickest solution would be to use the querystring
module. By requirting it on the top of your code, and then use it to send the agreement Object, as follows:
<%
const querystring = require('querystring');
for (var i=0; i < Agreements.length; i++) {
var Agreement = Agreements[i]
%>
<a href="/editAgreement/?<%= querystring.stringify(Agreement) %>" style="text-decoration: none; color: #333333">
<div class="well" align="center">
<h3>Roommate Agreement for Room #<%= Agreement.roomNumber%></h3>
<p><%= Agreement.roommate1%></p>
<p><%= Agreement.roommate2%></p>
</div>
</a>
<% } %>
本文标签:
版权声明:本文标题:How to pass an object from EJS to Node.js Router - Javascript, EJS, Node.js, MySQL - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745327993a2653681.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论