0

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

FreeLancer
  • 79
  • 1
  • 6
  • 13
  • Why don't you make it so all the information is stored in one schema instead of distributing them across different schemas? – Nicholas Nov 08 '21 at 00:55
  • It is separted with a purpose, thanks for your comment! :) – FreeLancer Nov 08 '21 at 01:06
  • I think i have found a great answer but i would like to know if doing 3 .save() is ok the solution is in this link: (is the second answer) https://stackoverflow.com/questions/55772477/how-to-implement-validation-in-a-separate-file-using-express-validator – FreeLancer Nov 08 '21 at 01:34
  • https://stackoverflow.com/questions/55772477/how-to-implement-validation-in-a-separate-file-using-express-validator the answer of Shivam Verma – FreeLancer Nov 08 '21 at 14:35

1 Answers1

0

validate them first, before save it into a database

try {

// check email function
function validateEmail(emailAdress) {
    let regexEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
    if (emailAdress.match(regexEmail)) {
        return true;
    } else {
        return false;
    }
}

//  check password function
function validatePassword(password) {
    return password.length <= 8 ?  false :  true; 
}


// check them here
if (!validateEmail(req.body.email)) return res.status(400).send("error email") 
if (!validatePassword(req.body.password)) return res.status(400).send("error password") 

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 {
  ....
}
Enis Gur
  • 89
  • 1
  • 9
  • Thanks for your anwser but i think this way is better to validate https://stackoverflow.com/questions/55772477/how-to-implement-validation-in-a-separate-file-using-express-validator answer off Shivam Verma – FreeLancer Nov 08 '21 at 14:35