Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

214
Views
How to show json message when validating Shema?

Pardon me if this question is silly! I have a problem like this: when I am validator in Shema and don't know how to code to display message on controller side. Hoping for everyone's help. Thank you very much!

userService.js

exports.findUserResgiter = async (email) => {
var result = null;
try{
    result = await User.findOne({
        email: email, 
    })
} catch(e){
}
return result; 

};

userController.js

exports.register = async (req, res, next)=>{
var email=req.body.email; 
var password=md5(req.body.password) 
var name=req.body.name 
var address=req.body.address 
var phone=req.body.phone 
var date_of_birth=req.body.date_of_birth 


var data = await userService.findUserResgiter(email);
console.log(data);
if(data === null){
    res.status(200).json({message: "null"});
    return User.create({ 
        name: name, 
        email: email, 
        password: password, 
        address: address, 
        date_of_birth: date_of_birth, 
        phone: phone, 
    }) 
}else{
    res.status(401).json({message: "This email already exists!"});
}

};

userModel.js

var validateEmail = function(email) {
var re = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
return re.test(email)

};

const UserShema = new Shema({
name: { type: String,  required: [true, 'Not blank'] },  
date_of_birth: { type: Date, required: [true, 'Not blank']},   
address: { type: String, required: [true, 'Not blank']},   
phone: { type: D, required: [true, 'Not blank']},     
email: { 
    type: String,     
    Required:  'Not blank',
    validate: [validateEmail, 'Not blank'],
        match: [/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/, 'Not blank'],
    index: {unique: true, dropDups: true}
    },  
password: { type: String , required: [true,  'Not blank']}, 

},

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!