【问题标题】:How to compare passwords in a mongoose schema?如何比较猫鼬模式中的密码?
【发布时间】:2021-08-08 17:37:06
【问题描述】:

如何比较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 }
);

【问题讨论】:

标签: javascript database mongodb mongoose model


【解决方案1】:

您可以在架构中使用验证器。 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',
},

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-31
    • 2016-10-01
    • 2020-04-25
    • 2021-04-08
    • 2014-06-01
    • 2012-02-02
    • 2013-01-13
    • 2016-08-05
    相关资源
    最近更新 更多