Answer the question
In order to leave comments, you need to log in
How to correctly control the incoming data before inserting/updating into the database?
I don’t know how else to call the topic, I don’t know what to do and why it happens, maybe I just don’t understand something (I just recently started trying nodejs), consider the problem on a simple user registration, a piece of code:
const register = (req, res, next) => {
const { name, email, password, passwordConfirmation } = req.body
async.waterfall([
cb => {
bcrypt.genSalt(10, (err, salt) => {
if (err) return cb(err);
bcrypt.hash(password, salt, cb);
})
}
], (err, results) => {
if (err) {
logger.error(err);
return ...
}
User.findOne({
where : {
$or : [
{name},
{email}
]
}
}).asCallback((err, model) => {
if (err) {
logger.error(err);
return ...
}
if (model && model.name == name) return ...name already exists
if (model && model.email == email) return ...email already exists
// create user
User.create({
name,
email,
password : results,
}, (err, model) => {
if (err) {
logger.error(err);
return ...
}
// done
return ...
})
})
})
}
Answer the question
In order to leave comments, you need to log in
I did not find good advice on what to do with conflicting requests, but for now I use transactions, maybe it will help someone.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question