medusajs / medusa

Building blocks for digital commerce
https://medusajs.com
MIT License
24.3k stars 2.37k forks source link

[medusa-plugin-contentful] Webhook error: Cannot read properties of undefined (reading 'contentType') #3005

Closed seminarian closed 1 year ago

seminarian commented 1 year ago

Bug report

Describe the bug

Sync back from Contentful via webhooks to Medusa server fails. Reponse returned has status code 400.

Information from the Web hook detail page in Contentful: Medusa dev server POST http://REMOVED_FOR_PRIVACY:9000/hooks/contentful 2023-01-11T21:05:00.055Z HTTP 400

Request Headers

{
  "X-Contentful-Topic": "ContentManagement.Entry.publish",
  "X-Contentful-Webhook-Name": "Medusa dev server",
  "Content-Type": "application/vnd.contentful.management.v1+json"
}
{
  "metadata": {
    "tags": []
  },
  "sys": {
    "type": "Entry",
    "id": "prod_01GPH9D2BMFG9Q2GYWAWQAAY2M",
    "space": {
      "sys": {
        "type": "Link",
        "linkType": "Space",
        "id": "4er9qaf67vsj"
      }
    },
    "environment": {
      "sys": {
        "id": "master",
        "type": "Link",
        "linkType": "Environment"
      }
    },
    "contentType": {
      "sys": {
        "type": "Link",
        "linkType": "ContentType",
        "id": "product"
      }
    },
    "createdBy": {
      "sys": {
        "type": "Link",
        "linkType": "User",
        "id": "5PRFv3fpx7EgztlvABz6JT"
      }
    },
    "updatedBy": {
      "sys": {
        "type": "Link",
        "linkType": "User",
        "id": "5PRFv3fpx7EgztlvABz6JT"
      }
    },
    "revision": 2,
    "createdAt": "2023-01-11T20:50:23.968Z",
    "updatedAt": "2023-01-11T21:04:59.810Z"
  },
  "fields": {
    "title": {
      "en-US": "Medusa Shirt english title1",
      "nl": "Medusa shirt dutch title1"
    },
    "subtitle": {
      "en-US": null
    },
    "handle": {
      "en-US": "medusa-shirt"
    },
    "thumbnail": {
      "en-US": {
        "sys": {
          "type": "Link",
          "linkType": "Asset",
          "id": "57YTcZFoZbDHNbiJxYIla2"
        }
      }
    },
    "description": {
      "en-US": "Reimagine the feeling of a classic T-shirt. With our men's Basic cotton T-shirts, everyday essentials no longer have to be ordinary."
    },
    "options": {
      "en-US": [
        {
          "id": "opt_01GPH9D2D2TZRX06MFHBXTF40R",
          "title": "Size",
          "medusaId": "opt_01GPH9D2D2TZRX06MFHBXTF40R",
          "metadata": null,
          "created_at": "2023-01-11T20:47:32.209Z",
          "deleted_at": null,
          "product_id": "prod_01GPH9D2BMFG9Q2GYWAWQAAY2M",
          "updated_at": "2023-01-11T20:47:32.209Z"
        }
      ]
    },
    "tags": {
      "en-US": []
    },
    "variants": {
      "en-US": [
        {
          "sys": {
            "type": "Link",
            "linkType": "Entry",
            "id": "variant_01GPH9D2FPVNGHK5D9QX9QA1SE"
          }
        },
        {
          "sys": {
            "type": "Link",
            "linkType": "Entry",
            "id": "variant_01GPH9D2KH1B1Y394YTWV8VTPC"
          }
        },
        {
          "sys": {
            "type": "Link",
            "linkType": "Entry",
            "id": "variant_01GPH9D2PM6746GP9JD9D9FRD2"
          }
        },
        {
          "sys": {
            "type": "Link",
            "linkType": "Entry",
            "id": "variant_01GPH9D2S4H6F2NBFAVRRW3K85"
          }
        }
      ]
    },
    "medusaId": {
      "en-US": "prod_01GPH9D2BMFG9Q2GYWAWQAAY2M"
    }
  }
}

Response headers

{
  "x-powered-by": "Express",
  "content-type": "text/html; charset=utf-8",
  "content-length": "74",
  "etag": "W/\"4a-f4kt9O7UGuvEnoSZooKa0mIW6y8\"",
  "set-cookie": [
    "connect.sid=s%3AeTIqRUEs6P5Bz_7zkp7-k_wKaj779qHy.pwcLjf1OP6tRgzmSIUpHkdyBwRxmMCKD5aOfLaObr4s; Path=/; Expires=Thu, 12 Jan 2023 07:05:00 GMT; HttpOnly"
  ],
  "date": "Wed, 11 Jan 2023 21:05:00 GMT",
  "connection": "close"
}

Response body

Webhook error: Cannot read properties of undefined (reading 'contentType')

System information

medusa-plugin-contentful: 1.2.4 @medusajs/medusa: 1.7.3 Node.js version: 17.1.0 Database: postgres Operating system: Docker version

Steps to reproduce the behavior

I've followed this guide: https://www.contentful.com/blog/create-a-composable-commerce-site-with-contentful-and-medusa/

The sync from Medusa to Contentful works.

Though the sync back to medusa, using Contentful hooks fails.

Expected behavior

After editing a product in Contentful see the changes synced to Medusa.

Additional context

Two locales are active in Contentful:

Baldrani commented 1 year ago

Well, after a bit of digging I realized that the problem comes from Contentful which changed the default content type. You have to set it manually.

image
olivermrbl commented 1 year ago

@Baldrani Thanks for pitching in. We'll make sure to add a note in our documentation :)

cc @shahednasser

RNCAT commented 1 month ago

Well, after a bit of digging I realized that the problem comes from Contentful which changed the default content type. You have to set it manually.

image

@Baldrani Thanks a lot, you save my life!