Next.js Discord

Discord Forum

Spamming POST requests Nextjs/Mongodb

Answered
Spectacled bear posted this in #help-forum
Open in Discord
Spectacled bearOP
Gold stored in database. User spams post request to buy one potion (Costs all his gold) he gets multiple potions because the post request doesnt wait for first request to finish.

How do I make sure that when a user send ten POST request to an api the first one gets resolved first. THanks
Answered by Spectacled bear
MONGO_DB = Create a token collection with a unique reference field linked to user.

POST = When request is sent create a token in database (Since its unique only the first post request that created the token will proceed).

ERROR CHECK = if token already exists return. // if token cannot be created return.

FINISH = Delete the token on any errors or before the post request finishes to allow another post request to happen.

Hope this isnt to conufsing.
View full answer

1 Reply

Spectacled bearOP
MONGO_DB = Create a token collection with a unique reference field linked to user.

POST = When request is sent create a token in database (Since its unique only the first post request that created the token will proceed).

ERROR CHECK = if token already exists return. // if token cannot be created return.

FINISH = Delete the token on any errors or before the post request finishes to allow another post request to happen.

Hope this isnt to conufsing.
Answer