Implementing the Update User Route

Listing 26.7 implements the updateUser route. This handler finds the user and then sets the values from the req.body.email and req.body.color properties that will come from the update form in the body of the POST request. Then the save() method is called on the User object, and the request is redirected back to the /user route to display the changed results.

Listing 26.7 users_controller.js-updateUser: Implementing the route to update users for the Express server


58 exports.updateUser = function(req, res){
59   User.findOne({ _id: req.session.user })
60   .exec(function(err, user) {
61     user.set('email', req.body.email);
62     user.set('color', req.body.color);
63     user.save(function(err) {
64       if (err){
65         res.sessor.error = err;
66       } else {
67         req.session.msg = 'User Updated.';
68       }
69       res.redirect('/user'),
70     });
71   });
72 };


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.233.153