Reputation: 161
I'm trying to pass a variable from my server.js file to HTML but the variable will not show up inside my EJS file. I must be missing something as it's working on another route (with another EJS file) but I can't see the form or the variable I'm trying to pass over. If I put anything outside the form it shows up but the form itself won't show up and I can console.log from my server and it shows I am finding the variable from my Models and it passes the callback error test. I've tried using working EJS code in place of the form and that won't show either. Again it is going to the right place as I can put anything outside the form.
Also I'm not getting an error, the form nor the variable simply aren't showing up in the EJS file. If I change anything inside my <% %> tags (like below):
<% for(var i=0;i<turtles.length;i++){ %>
It causes an error saying (example)turtle(/example) is not defined. So it knows it is there.
Thank you in advance.
My server.js file:
edit: function(req, res){
Turtle.findOne({_id: req.params.id}, function(err, turtle){
if(err){}
else{
console.log(turtle._id);
res.render("edit", {turtles:turtle})}
})
},
My edit.ejs:
<% for(var i=0;i<turtles.length;i++){ %>
<h3><%= turtles[i].name %></h3>
<form action='/turtles/<%= turtles[i]._id %>' method='post'>
<p>Name: <input type='text' name='name' value="<%= turtles[i].name %>"></p>
<p>Favorite color: <input type='text' name='favecolor' value="<%= turtles[i].favecolor %>"></p>
<p><input type='submit' value='Edit'></p>
</form>
<% } %>
Upvotes: 1
Views: 223
Reputation: 8642
you're using findOne
method by mongo driver which returns a single document, an object, if your desired result is to have an array , allowing the iteration in the .ejs template, try using find
Upvotes: 2