Meherab Hassan
Meherab Hassan

Reputation: 3

Mongoose Validation error in all the fields

I have a Mongoose Schema ready but when I call it with my api it gives out validation errors in all of the required fields

this is my schema object

const userSchema = new mongoose.Schema({
    name: {
        type: String,
        required: [true, 'Please use a name']
    },

    email: {
        type: String,
        required: [true, 'Please use a valid e-mail'],
        unique : true,
        lowercase: true,
        validator: [validator.isEmail, 'Please provide a valid e-mail']
    },
    password: {
        type: String,
        required: [true, 'Please provide a password for your profile'],
        minlength: 6
    },

    passwordConfirm: {
        type: String,
        required: [true, 'Please confirm your password']
    }
})


const User = mongoose.model('User', userSchema)


the req. body i sent with the api through JSON is

{
    "name": "user1",
    "email": "user1@gmail.com",
    "password": "pass1234",
    "passwordConfirm": "pass1234"
}

I was trying to make a new user through the api and model throuth the controller

exports.signup = async (req, res, next) => {
    try {
        const newUser = await User.create(req.body)
        console.log('in the newUser');
        res.status(201)
           .json({
            status: 'success',
            data: {
                user: newUser
            } 
            
        }) 

    }catch (err){
        res.status(400).json({
            status: 'fail',
            message: err
        })
    }
}
then I get the following errors in postman


{ "status": "fail", "message": { "errors": { "passwordConfirm": { "name": "ValidatorError", "message": "Please confirm your password", "properties": { "message": "Please confirm your password", "type": "required", "path": "passwordConfirm" }, "kind": "required", "path": "passwordConfirm" }, "password": { "name": "ValidatorError", "message": "Please provide a password for your profile", "properties": { "message": "Please provide a password for your profile", "type": "required", "path": "password" }, "kind": "required", "path": "password" }, "email": { "name": "ValidatorError", "message": "Please use a valid e-mail", "properties": { "message": "Please use a valid e-mail", "type": "required", "path": "email" }, "kind": "required", "path": "email" }, "name": { "name": "ValidatorError", "message": "Please use a name", "properties": { "message": "Please use a name", "type": "required", "path": "name" }, "kind": "required", "path": "name" } }, "_message": "User validation failed", "name": "ValidationError", "message": "User validation failed: passwordConfirm: Please confirm your password, password: Please provide a password for your profile, email: Please use a valid e-mail, name: Please use a name" } }

Upvotes: 0

Views: 427

Answers (1)

Paul-Marie
Paul-Marie

Reputation: 1063

Just use required: true in your schema

If the validation fail, an error will be throw in:

const newUser = await User.create(req.body);

so you need to catch it and return an appropriate message to the user here (no in the model validation)

Upvotes: 0

Related Questions