everytime i register with same email on register page it still record on my mongodb whereas i expect it to not recorded
i try to write unique:true on email var at User.js like this
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);
})
i added an unique index on my database by writing
db.users.createIndex({ email: 1 }, { unique: true })
so that mongoDB will enforce the uniqueness constraint on the email field going forward, ensuring that no new duplicate emails can be inserted into the collection.