[英]How to compare passwords in a mongoose schema?
How can I compare password
and password_confirmation
, and if they don't match then I'll print "Passwords don't match"
to the error message?如何比较password
和password_confirmation
,如果它们不匹配,那么我将在错误消息中打印"Passwords don't match"
?
const userSchema = new Schema(
{
username: {type: String, required: [true, "Please enter a username"], unique: [true, "Username taken"]},
password: {type: String, required: true, minLength: [8, "Minimum password lenth is 8"]},
password_confirmation: {type: String}
},
{ timestamps: true }
);
You can use validator in your schema.您可以在架构中使用验证器。 https://www.npmjs.com/package/validator https://www.npmjs.com/package/validator
passwordConfirm: {
type: String,
required: [true, 'Please confirm ypur password'],
validate: {
// this only works on CREATE and SAVE!!!!
validator: function (el) {
return el === this.password;
},
message: 'Password are not the same',
},
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.