javascript - Custom Validator in express -
i'm trying build custom validator here checking if username exists or not. @ time of form submission i'm getting username exist message when there no data in db.
app.use(validator({ errorformatter: function(param, msg, value) { var namespace = param.split('.') , root = namespace.shift() , formparam = root; while(namespace.length) { formparam += '[' + namespace.shift() + ']'; } return { param : formparam, msg : msg, value : value }; }, customvalidators: { isusername: function(value) { return new promise((resolve,reject) => { user.findone({'username': username}, function(err, results) { if (err) throw err; if(user == null) { resolve(); } else { reject(); } }); }); } } })); middleware
req.check('username','username exist').isusername(); req.asyncvalidationerrors().then(() => { console.log("no error"); next(); }).catch((errors) => { console.log("errors"); if(errors){ res.send(errors[0]); } });
Comments
Post a Comment