Reputation: 2399
I am using express-validator and have made one custom validation with mongoose database find, now I can't send withMessage. Here is my code althought process stop at Error but it does not show message in Json, but when user created it is show all.
body('mobile', 'Mobile number is required')
.custom((value, {req, loc, path}) => {
User.countDocuments({mobile: value}, function (err, c) {
if (err) {
console.log('An error happen in db')
}
if (c > 0) {
throw new Error('Mobile already exists finally')
} else {
return value
}
})
return value
})
.withMessage('Mobile already exists'),
Following is log of console
functions: Beginning execution of "app"
> events.js:298
> throw er; // Unhandled 'error' event
> ^
>
> Error: Mobile already exists finally
> at /Users/kamal/Documents/personal/asghar/codes/functions/app/controllers/users_controller.js:117:23
> at /Users/kamal/Documents/personal/asghar/codes/functions/node_modules/mongoose/lib/model.js:4849:16
> at /Users/kamal/Documents/personal/asghar/codes/functions/node_modules/mongoose/lib/model.js:4849:16
> at /Users/kamal/Documents/personal/asghar/codes/functions/node_modules/mongoose/lib/helpers/promiseOrCallback.js:24:16
> at /Users/kamal/Documents/personal/asghar/codes/functions/node_modules/mongoose/lib/model.js:4872:21
> at /Users/kamal/Documents/personal/asghar/codes/functions/node_modules/mongoose/lib/query.js:4379:11
> at /Users/kamal/Documents/personal/asghar/codes/functions/node_modules/kareem/index.js:135:16
> at processTicksAndRejections (internal/process/task_queues.js:79:11)
> Emitted 'error' event on Function instance at:
> at /Users/kamal/Documents/personal/asghar/codes/functions/node_modules/mongoose/lib/model.js:4851:13
> at /Users/kamal/Documents/personal/asghar/codes/functions/node_modules/mongoose/lib/helpers/promiseOrCallback.js:24:16
> [... lines matching original stack trace ...]
I need to add if condition near return value
but problem is call back does not bring me value of c here, in above it is comign correct and even stop due to Error raising, but I don't want to raise error instead want to go further withMessage('Mobile already exists')
I am sure doing mistake in callback. Please suggest solution
Upvotes: 0
Views: 70
Reputation: 2399
After playing and seeing many many documentation I solved this by doing following, in my User model I put following code, as my model is actualy mongoose module.
UserSchema.statics = {
isValidMobile(mobile) {
console.log(` Searching obile ${mobile} number `)
return this.findOne({mobile: mobile}).then((result) => {
if (result) throw new Error('Mobile Number already exists')
})
},
}
Now in my validation I put following line instead of above lines.
body('mobile', 'Mobile number is required')
.custom((val) => User.isValidMobile(val))
It worked, and I am getting proper messages with whole JSON, as it seem custom require proper true/false reply so my method is replying just true or false and it is working fine with correct message, but the message is being used came from User model class not from validation, but that works. Thank you for responses.
Upvotes: 0
Reputation: 563
This should work
body("mobile").custom(value => {
return User.countDocuments({ mobile: value })
.then(count => {
if (count > 0) return Promise.reject("Mobile number already exists");
})
.catch(error => console.error(error.message));
});
From express-validator documentation
Custom validators may return Promises to indicate an async validation (which will be awaited upon), or throw any value/reject a promise to use a custom error message. Note: if your custom validator returns a promise, it must reject to indicate that the field is invalid.
Upvotes: 1