I am trying to add a validation method to my ExpressJS app, however it does not quite seem to work out... I can currently change the username, however if it has an empty String, the app crashes, although it is supposing to send an error. Any suggestions?
var validatePresenceOf = function(val, func) {
if (typeof val === 'undefined' || val.length < 1) {
res.json({
errors: {
userName: "Your name can't be blank"
}
});
} else {
return func;
}
};
validatePresenceOf(req.body.userName, User.findByIdAndUpdate( currentUser._id, {
local: {
name : req.body.userName
}
}, function (err, user) {
if (err) throw err;
res.json({
user : user
});
}));
Express gives error when setHeader method called twice. Maybe you forgot or didn't use the "return" when validation method gives the error! Please check this. I'm believing express validator is better solution for you