【发布时间】:2020-11-25 09:41:58
【问题描述】:
您好,我是一个尝试学习节点的初学者 我正在尝试验证用户在注册页面中输入的数据 这是我的猫鼬模式
const schema = mongoose.Schema({
fname:{type:String,
required:[true,"enter a first name"]},
lname:{type:String,
required:[true,"enter a last name "]},
email:{type:String,
required:[true,"email is required"],
unique:true,
trim:true,
lowercase:true,
validate(value){
if(!validator.isEmail(value)){
throw new Error("Email is invalid");
}
}
},
pass:{type:String,required:[true,"please enter a passkey"],minlength:[8,"enter atleast 8 characters"]}
});
如您所见,我正在尝试验证某些字段并检查错误
usersave.save((err,doc)=>{
if(err){
console.log(err);
res.render("register",{err:err}
}
else{console.log(doc);res.send("success")}
});
如果用户在注册表单中输入错误数据,我会在控制台中收到以下错误消息:-
Error: users validation failed: fname: enter a first name, lname: enter a last name , pass: please enter a passkey, email: Error, expected `email` to be unique. Value: `ar@gmail.com`
at ValidationError.inspect (D:\.UC\eb Development\react-practice\authentication\node_modules\mongoose\lib\error\validation.js:47:26)
at formatValue (internal/util/inspect.js:718:31)
at inspect (internal/util/inspect.js:287:10)
at formatWithOptions (internal/util/inspect.js:1910:40)
at Object.Console.<computed> (internal/console/constructor.js:299:10)
at Object.log (internal/console/constructor.js:309:61)
at D:\.UC\eb Development\react-practice\authentication\index.js:75:21
at D:\.UC\eb Development\react-practice\authentication\node_modules\mongoose\lib\model.js:4824:16
at D:\.UC\eb Development\react-practice\authentication\node_modules\mongoose\lib\helpers\promiseOrCallback.js:16:11
at D:\.UC\eb Development\react-practice\authentication\node_modules\mongoose\lib\model.js:4847:21
at D:\.UC\eb Development\react-practice\authentication\node_modules\mongoose\lib\model.js:494:16
at D:\.UC\eb Development\react-practice\authentication\node_modules\kareem\index.js:246:48
at next (D:\.UC\eb Development\react-practice\authentication\node_modules\kareem\index.js:167:27)
at next (D:\.UC\eb Development\react-practice\authentication\node_modules\kareem\index.js:169:9)
at Kareem.execPost (D:\.UC\eb Development\react-practice\authentication\node_modules\kareem\index.js:217:3)
at _handleWrapError (D:\.UC\eb Development\react-practice\authentication\node_modules\kareem\index.js:245:21) {
errors: {
fname: ValidatorError: enter a first name
at validate (D:\.UC\eb Development\react-practice\authentication\node_modules\mongoose\lib\schematype.js:1217:13)
at D:\.UC\eb Development\react-practice\authentication\node_modules\mongoose\lib\schematype.js:1200:7
at Array.forEach (<anonymous>)
at SchemaString.SchemaType.doValidate (D:\.UC\eb Development\react-practice\authentication\node_modules\mongoose\lib\schematype.js:1145:14)
at D:\.UC\eb Development\react-practice\authentication\node_modules\mongoose\lib\document.js:2392:18
at processTicksAndRejections (internal/process/task_queues.js:79:11) {
properties: [Object],
kind: 'required',
path: 'fname',
value: '',
reason: undefined,
[Symbol(mongoose:validatorError)]: true
},
lname: ValidatorError: enter a last name
at validate (D:\.UC\eb Development\react-practice\authentication\node_modules\mongoose\lib\schematype.js:1217:13)
at D:\.UC\eb Development\react-practice\authentication\node_modules\mongoose\lib\schematype.js:1200:7
at Array.forEach (<anonymous>)
at SchemaString.SchemaType.doValidate (D:\.UC\eb Development\react-practice\authentication\node_modules\mongoose\lib\schematype.js:1145:14)
at D:\.UC\eb Development\react-practice\authentication\node_modules\mongoose\lib\document.js:2392:18
at processTicksAndRejections (internal/process/task_queues.js:79:11) {
properties: [Object],
kind: 'required',
path: 'lname',
value: '',
reason: undefined,
[Symbol(mongoose:validatorError)]: true
},
pass: ValidatorError: please enter a passkey
at validate (D:\.UC\eb Development\react-practice\authentication\node_modules\mongoose\lib\schematype.js:1217:13)
at D:\.UC\eb Development\react-practice\authentication\node_modules\mongoose\lib\schematype.js:1200:7
at Array.forEach (<anonymous>)
at SchemaString.SchemaType.doValidate (D:\.UC\eb Development\react-practice\authentication\node_modules\mongoose\lib\schematype.js:1145:14)
at D:\.UC\eb Development\react-practice\authentication\node_modules\mongoose\lib\document.js:2392:18
at processTicksAndRejections (internal/process/task_queues.js:79:11) {
properties: [Object],
kind: 'required',
path: 'pass',
value: '',
reason: undefined,
[Symbol(mongoose:validatorError)]: true
},
email: ValidatorError: Error, expected `email` to be unique. Value: `ar@gmail.com`
at validate (D:\.UC\eb Development\react-practice\authentication\node_modules\mongoose\lib\schematype.js:1217:13)
at D:\.UC\eb Development\react-practice\authentication\node_modules\mongoose\lib\schematype.js:1192:24
at processTicksAndRejections (internal/process/task_queues.js:97:5) {
properties: [Object],
kind: 'unique',
path: 'email',
value: 'ar@gmail.com',
reason: undefined,
[Symbol(mongoose:validatorError)]: true
}
},
_message: 'users validation failed'
}
我希望能够在注册页面中通知用户他们做错了什么 我收到错误消息并将错误对象传递到注册页面以重新渲染 这次显示错误
当我将错误对象传递给 register.ejs 时,它包含以下信息
ValidationError: fname: enter a first name, lname: enter a last name , pass: please enter a passkey, email: Error, expected `email` to be unique. Value: `ar@gmail.com`
我想在注册页面中只显示错误消息,我该如何从上述信息中做到这一点? 或者有没有其他方法可以在前端显示错误?
【问题讨论】:
-
最好在模型模式中设置“required: true”,然后在路由器中管理验证和消息传递。请参阅下面的答案。
标签: node.js forms validation mongoose ejs