I am working on an e-shop using Strapi as the backend and Next.js as the frontend. I encountered an issue where, if multiple users add products to their basket simultaneously (simulated using two different browsers), a race condition occurs. The user who loses the race ends up with an empty basket.
Current Implementation:
Fetching Products:
I retrieve the product collection from Strapi to display all products on the webpage:{ "kind": "collectionType", "collectionName": "products", "info": { "singularName": "product", "pluralName": "products", "displayName": "Product" }, "attributes": { "title": { "type": "string", "required": true }, "price": { "type": "decimal", "required": true }, "user_cart": { "type": "relation", "relation": "manyToOne", "target": "api::user-cart.user-cart", "inversedBy": "product" } } }
Adding Product to User Cart:
{ "kind": "collectionType", "collectionName": "user_carts", "info": { "singularName": "user-cart", "pluralName": "user-carts", "displayName": "User Cart" }, "attributes": { "product": { "type": "relation", "relation": "oneToMany", "target": "api::product.product", "mappedBy": "user_cart" } } }
Problem:
When I check the user who lost the race, their product array is empty, while the winning user gets the relation data correctly.
Why am I not receiving the related product information (e.g., product title) for the losing user?