D
D
daminik002020-06-03 23:10:09
Node.js
daminik00, 2020-06-03 23:10:09

How to handle 2 requests at the same time in node.js?

Let's say there is a floor in the database that is responsible for the number of players in the team. The maximum number of players is 5. There are currently 4 players in the team.

The server receives a request to add a player to the team, we check players < 5 and if true, then add the player to the team. The code that is executed after checking players < 5 takes time to save the data to the database. And so, for example, another request comes to the server to add a player to the team and again checks players < 5. Since the previous request has not yet managed to save the data to the database, this check will also be true and the user will be added to the database.

Is this possible, and if so, how can it be avoided?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2020-06-03
@bingo347

https://docs.mongodb.com/master/core/transactions/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question