簡體   English   中英

如何比較 mongoose 架構中的密碼?

[英]How to compare passwords in a mongoose schema?

如何比較passwordpassword_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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM