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.
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 };