Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There are at least two problems here that would cause this to not work:</p> <ol> <li><p>The name of the input passed by your form is <code>todo.name</code>, and you're referencing it as <code>req.body.name</code> in the route.</p></li> <li><p>mongoose models are instantiated with an attributes object, but you're just giving it a string (which, actually, is null currently because of the first issue).</p></li> </ol> <p>So for your route to work it would look more like this:</p> <pre><code>app.post("/todo", function (req, res) { var todo = new Todo({name: req.body["todo.name"]}); todo.user = req.user._id; // ... }); </code></pre> <p>If you wanted to pass todo attributes as a parameter object, you'd want to name them with brackets <code>todo[name]</code>, rather than dots. This would result in the todo attributes being on object on the req.body, e.g.:</p> <pre><code>app.post("/todo", function (req, res) { console.log(req.body.todo); //=&gt; { name: "whatever" } // ... which means you could do var todo = new Todo(req.body.todo); todo.user = req.user._id; // ... }); </code></pre> <p>Some other things you might want to change:</p> <ol> <li><p>As @NilsH points out, you don't want to pass the user id in the form, as that would allow anyone to make a todo for anyone else just by knowing their ID. Rather since you're using passport, make use of the user in the session. You should have access to the user ID through the passport determined user, like <code>req.user._id</code>. I added this to both examples above.</p></li> <li><p>The <code>type</code> of your form input is <code>todo.name</code>. It should be <code>text</code> (that's what the browser is treating it as anyway).</p></li> <li><p>Not necessarily an error, but model names are conventionally capitalized. This also solves an issue your code has above in that you're redefining <code>todo</code> when you say <code>var todo = new todo(...)</code>.</p></li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload