0

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.

Shak Daniel
  • 217
  • 4
  • 11
  • There is a similar question answered at http://stackoverflow.com/questions/2608913/how-can-i-write-reusable-javascript that should be helpful. – Bronwen Apr 01 '15 at 21:20

2 Answers2

3

Looks like there are only 2 things that differ, and they are strings. Pass them in as parameters.

exports.updateAccount = function(req, res, next, path, message) { //add 2 parameters
    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: message //message param
            });
            res.redirect(path); //path param
        });
    });
};
Sterling Archer
  • 22,070
  • 18
  • 81
  • 118
1

You can create a generic flow, and bind with specific parameters, like below

function updateUser(message, url, req, res, next ){

    var body = req.body;

    User.findById(req.params.rid, function(err, user) {
        if (err) {
            return next(err);
        }

        user.email = body.email || '';
        user.firstname = body.firstname || '';
        user.lastname = body.lastname || '';

        user.save(function(err) {
            if (err) {
                return next(err);
            }
            req.flash('success', {
                msg: message
            });
            res.redirect(url);
        });
    });

}

exports.updateUser = updateUser.bind(this, "User information updated.", "/users");
exports.updateAccount = updateUser.bind(this, "Profile information updated.", "/account");
Luan Castro
  • 1,184
  • 7
  • 14