angularjs - Node.js: req.params vs req.body -
I mean the code from some different tutorials has been cobbling together to build a basic agenda app with the pile, the node Using, Express, Angular and Moodbot was a tutorial in which an API was created for GET, POST and DELETE works, but ignored the post. Therefore, I have taken it as a challenge to write a work that will update an existing work. When I got the job to work, I got an error in which the receiparms were included which I did not understand.
Relevant code:
node:
app.js
app. ('/ Api / todos /: _id', ngRoutes.update);
which leads to:
exports.update = function (req, res) {var user_id = req.cookies? Req.cookies.user_id: Undefined; Todo.findByIdAndUpdate (req.params._id, {$ set: {updated_at: Date.now (), content: req.body.formText}}, function (hey, to function) {if (mistake) res.send ( Mistake) Todo.find ({user_id: user_id}, function (mistake, todos) {if (err) res.send (err); res.json (todos);});}); };
angular:
$ scope.update = function (id) {$ http.put ('/ api / todos /' + ID, this. Todo) .success (data) {console.log (data); $ scope.todos = data;} )error (function (data) {console.log ('error:' + data);}); }; Z / HTML: form (ng-submit = "update (todo._id)") input.update-form (ng-show = "todo.updating" type = "text", Name = "content", ng model = "todo.formText" placeholder = "{{todo.content}}")
This function works fine, it updates in the underlying, And with the updated list to reload the entire list on the page.
However, if in the node code, I
content: req.body.formText
to content: req.params.formText
I get the following error as my HTTP response:
object {message: "for string The string failed in the "path" content "", "undefined", name: "CastError", type: "string", path: "content"}
Anywhere,
req.params._id
OK to recover Todo's '_id' property It works and uses it to find the appropriate documents in the database. In addition, when viewing the request in Firefox's Developer Tools, the Todd object appears in JOSF format under the "PRAM" tab.
< P> Why does this happen? What is the difference between using Req.params versus req.body, and why does not the other work and why?
Reiki parations is for root parameter, not Process your form data. In that path you have the only absolute
_id
:
app. Input ('/ api / todos /: _ id', ...)
Comments
Post a Comment