i have 3 tables "users" , "users_passwords", "users_emails"
"users" is the main table users_password and users_email are relashionship OnetoOne with tables users
I am using this scheme in the models:
users_email :
const UserEmail = new mongoose.Schema({
user: {
type : mongoose.Schema.Types.ObjectId,
ref: 'User'
},
email: {
type : String,
require: [true, "Required E-mail"],
validate: [isEmail, "Please enter a valid email"]
}
})
users_password:
const UserPassword = new mongoose.Schema({
user: {
type : mongoose.Schema.Types.ObjectId,
ref: 'User'
},
password: {
type : String,
require: [true, "Required Password"],
minlength: [8, "Minimum passowrd lenght is 8 characters"]
}
})
Than in my controller i have the function regist
try{
const email = new UserEmail({
email: req.body.email
})
const auth = new UserAuth({
password: req.body.password,
})
const user = new User({
email: email._id,
auth: auth._id,
})
await email.save()
await auth.save()
const result = await user.save()
const {...data} = await result.toJSON()
res.send(data)
}catch{
....
}
What is happening is if the email is OK and valid it goes to password and if it fails on password will save the UserEmail.
What i want is if in any one of the tables fails don't execut the save!
I am realy new at express and mongoDb,i am learning and this is project to scholl thanks for help