Javascript 如何在mongoose模式中比较密码?

Javascript 如何在mongoose模式中比较密码?,javascript,database,mongodb,mongoose,model,Javascript,Database,Mongodb,Mongoose,Model,如何比较密码和密码确认,如果它们不匹配,我将在错误消息中打印“密码不匹配” const userSchema = new Schema( { username: {type: String, required: [true, "Please enter a username"], unique: [true, "Username taken"]}, password: {type: String, required: true, min

如何比较
密码
密码确认
,如果它们不匹配,我将在错误消息中打印
“密码不匹配”

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 }
);

您可以在模式中使用验证器。


}

这是否回答了您的问题?不,答案可能已经过时了@艾哈迈达比
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',
},