每次我在注册页面上用相同的邮箱注册,它仍然记录在我的mongodb上,而我希望它不记录
我try 在User.js的邮箱变量上写下Unique:True,如下所示
import bcrypt from 'bcrypt';
import { Schema, model, models } from "mongoose";
const UserSchema = new Schema({
email: {type: String, required: true, unique: true},
password: {
type: String,
required: true,
validate: pass => {
if(!pass?.length || pass.length < 5){
new Error('password setidaknya harus 5 karakter');
return false;
}
},
},
},{timestamps: true});
UserSchema.post('validate', function (user){
const notHashedPassword = user.password;
const salt = bcrypt.genSaltSync(10);
user.password = bcrypt.hashSync(notHashedPassword, salt);
})