mercredi 1 avril 2015

How to make these two similar blocks codes reusable?

I have two similar codes and I think it could be reusable but not sure where to start. If you can show me and example or link me to an article that would be fine.


I have this one block that updates a user info.



exports.updateUser = function(req, res, next) {
User.findById(req.params.id, function(err, user) {
if (err) {
return next(err);
}
user.email = req.body.email || '';
user.firstname = req.body.firstname || '';
user.lastname = req.body.lastname || '';

user.save(function(err) {
if (err) {
return next(err);
}
req.flash('success', {
msg: 'User information updated.'
});
res.redirect('/users');
});
});
};


and this block that updates my user account info.



exports.updateAccount = function(req, res, next) {
User.findById(req.user.id, function(err, user) {
if (err) {
return next(err);
}
user.email = req.body.email || '';
user.firstname = req.body.firstname || '';
user.lastname = req.body.lastname || '';

user.save(function(err) {
if (err) {
return next(err);
}
req.flash('success', {
msg: 'Profile information updated.'
});
res.redirect('/account');
});
});
};


How can I make a reusable update.


Aucun commentaire:

Enregistrer un commentaire