Error Details
[Error [ValidationError]: "name" failed custom validation because Cannot read property 'presence' of undefined] {
_original: {
name: 'John Doe',
email: '[email protected]',
password: '12324sgs4sfsf674yhlh'
},
details: [
{
message: `"name" failed custom validation because Cannot read property 'presence' of undefined`,
path: [Array],
type: 'any.custom',
context: [Object]
}
]
}
Schema
const Joi = require('@hapi/joi');
//Validation
const schema = Joi.object().keys({
name: Joi.string().min(6).required,
email: Joi.string().min(6).email().required,
password: Joi.string().min(8).required,
});
Implementation
router.post('/register', async(req, res) => {
try {
const value = await schema.validateAsync({name: req.body.name, email: req.body.email, password: req.body.password});
console.log(value);
} catch (err) {
console.log(err);
}
// const {error} = schema.validate({name: req.body.name, email: req.body.email, password: req.body.password});
// console.log(error);
const user = new User({
name: req.body.name,
email: req.body.email,
password: req.body.password,
});
try {
const savedUser = await user.save();
res.send(savedUser);
} catch (error) {
res.send(error);
};
});
Solved
hey man, do you remember how you solved the issue? im having the same error
@najdi123 using .required instead of .required() may cause this.
@najdi123 using
.requiredinstead of.required()may cause this.
Yea, that was my problem too, thanks for replying
@najdi123 using
.requiredinstead of.required()may cause this.
Same here.
Had the same issue using TypeScript / ES6. The solution was to use import * as Joi from 'joi'; instead of import Joi from 'joi'; as Joi doesn't seem to have a default Export
Most helpful comment
@najdi123 using
.requiredinstead of.required()may cause this.