I have my user model.
// models/user.js
var mongoose=require("mongoose");
var Schema=mongoose.Schema;
var userSchema=new Schema({
name:{
type:String,
required:"Es necesario un nombre",
maxlength:[10,"Nombre muy largo max 10"]
},
lastName:{
type:String,
required:"Es necesario el apellido",
maxlength:[10,"Apellido muy largo"]
},
userName:{
type:String,
unique:true,
require:"Es necesario un username",
},
password:{
type:String,
require:"Es necesario un password",
validate:{
validator:function(pass){
if( this.password_confirmation!=undefined)
return this.password_confirmation==pass;
else{
return true;
}
},
message:"Las contraseñas son diferentes"
}
},
email:{
type:String,
require:"Es necesario un email"
}
})
userSchema.virtual("password_confirmation").get(function(){
return this.p_c
}).set(function(password){
this.p_c=password;
})
var User=mongoose.model("User",userSchema);
module.exports=User;
An endpoint to log me in.
// routes/user.js
router.route("/login").get(login.login_get).post(login.login_post);
// login_post
var login_post=function(req,res,next){
if(!req.body.password || !req.body.userName){
res.send("Error");
}
User.findOne({userName:req.body.userName,password:req.body.password},function(err,us){
if(err)console.log(String(err));
console.log("Usuario encontra",us);
res.send(us);
});
}
What I want to do is the following, validate from the model before entering User.findOne, for example something like that.
var user=new User(req.body,function(err){
if(err)res.send("Error",String(err));
});
User.findOne({userName:user.userName,password:user.password},function(err,us){
if(err)console.log(String(err));
console.log("Usuario encontra",us);
res.send(us);
});
to avoid using this and validate from the model.
if(!req.body.password || !req.body.userName){
res.send("Error");
}
or else you can like that maybe something like that.
var validate=function(model,next){
if(//NO SE QUE IRIA PARA VALIDAR EL MODELO//){}
next("//Algo como los errores del modelo model.err//);
}
var user=new User(req.body);
validate(user,function(err){
if(err){
console.log(String(err));
res.send("Hubo un error validando los datos",err);
}
}
}
This is to be able to search for users, since to create it is not necessary to do something extra, because if I throw out the errors that I have put.
Since when doing
User.findOne({userName:req.body.userName,password:req.body.password},function(err,us){
if(err)console.log(String(err));
console.log("Usuario encontra",us);
res.send(us);
});
without the validation it throws only that it did not find the user, and no error if for example I do not put password.
For md
I did not know about DTO, as soon as you validate it looks good, then if I should create a DTO schema in specific cases, but what I was doing is the following, what I was doing in my examples was to bring the error pull , which defined in the model because for example if you put something like
User.create(req.body,function(err,us){
if(err) console.log(String(err));
console.log(us);
});
This does bring me the set of errors, for example if passwords do not match, or fill a field, I wanted to do something for the findOne, but this does not bring me the error pull (defined in userSchema) to show them in a toast, but rather does not detect any error therefore does not return anything.
With what you set me to create a DTO I could do it, but the question is that I wanted to minimize the code I put, in addition to the libraries, so my query.
If the response codes went away, I still need to optimize the code better.