Answer the question
In order to leave comments, you need to log in
How to correctly add an entry to the collection so that there is no duplicate key error?
Hello!
There is the following code, which checks if there is another user with the same login before registering a user:
const candidate = await UserModel.findOne({login})
if (candidate) {
throw ApiError.BadRequest(`Пользователь с логином ${login} уже существует`)
}
const user:UserDocument = await UserModel.create({login, email, password: hashPassword, activationLink});
const candidate = await UserModel.findOne({login})
let user:UserDocument;
if (password==='start')
setTimeout(async ()=> {user = await UserModel.create({login, email, password: hashPassword, activationLink})}, 20000)
else
user = await UserModel.create({login, email, password: hashPassword, activationLink})
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question