[英]How to compare passwords in a mongoose schema?
如何比較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 }
);
您可以在架構中使用驗證器。 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.