Tuesday, 12 November 2019

What do you think of this piece of code?

https://github.com/gothinkster/node-express-realworld-example-app/blob/master/routes/api/users.jsI want you to check this specifically?router.put('/user', auth.required, function(req, res, next){ User.findById(req.payload.id).then(function(user){ if(!user){ return res.sendStatus(401); } // only update fields that were actually passed... if(typeof req.body.user.username !== 'undefined'){ user.username = req.body.user.username; } if(typeof req.body.user.email !== 'undefined'){ user.email = req.body.user.email; } if(typeof req.body.user.bio !== 'undefined'){ user.bio = req.body.user.bio; } if(typeof req.body.user.image !== 'undefined'){ user.image = req.body.user.image; } if(typeof req.body.user.password !== 'undefined'){ user.setPassword(req.body.user.password); } return user.save().then(function(){ return res.json({user: user.toAuthJSON()}); }); }).catch(next); }); I am wondering if you would put the large chunk of code in a service layer and how you would do it.

Submitted November 12, 2019 at 04:53PM by jesusscript

No comments:

Post a Comment