1

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>
<% } %>

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.