I have created a route '/api/checkUsername'. Which action is to check if there is any user exists with the same username. If not Save the new UserName in users collection and update the users count in another collection.
For the route, I have a function as
async function(username) {
IsUserNameExists = await db.findOne({ 'username': username });
if(IsUserNameExists) {
res.send({ message: 'username already exists');
}else{
await user.save() //to save user in DB
await updateUserCount() //update user count in collection 2
res.send({ message: 'user saved successfully' });
}
}
This works fine with all use cases. But a case like if 2 users ['user1', 'user2'] user check for the same username eg: Tony_Stark at the same second or with few milli-second difference.
eg user1 and user2 hits /api/checkUsername at the same time or with millisecond difference. user 1 finds that username Tony_Stark is available and saving his username, but in between saving the user name the user2 also looks for the username Tony_Stark which has not been saved by user1, So it makes that username available for user2 also. Which creates a duplicate entry in my collection. How do I avoid this?