最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Race Condition in Strapi Prevents Relation Field from Being Filled in User Cart - Stack Overflow

programmeradmin2浏览0评论

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:

  1. 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" }
      }
    }
    
  2. 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?

发布评论

评论列表(0)

  1. 暂无评论