Straight Forwardly:
I am clicking the button two times from the same machine(user) or from the different machine(user) within the span of 1 seconds. It's creating two documents (duplicates).
In short may be how to handle multiple API calls parallelly or concurrently.
What I suspect:
- Nodejs collects the API call ->
db.find(name)-> Not Found-> Creating New document (Its running)
Before the above document creation done. Nodejs started executing the next API call.
- Nodejs collects the API call ->
db.find(name)-> Not Found -> Creating New document.
Example Code: Here two account with same name is created.
const userPresent = await User.findOne({
phoneNumber: data.phoneNumber,
});
if (userPresent) {
throw new CustomError("OIC_ERROR_00027", "User already present");
}
// new account created
const newAccount = await new Account({
name: data.name,
}).save();