shopify/shopify-api 中的 Webhooks 使用 nodejs

Webhooks in shopify/shopify-api using nodejs

我正在测试节点 shopify-api,我注意到 server.js 中有一个代码注册了 APP_UNINSTALLED webhook。所以,我添加了下面的代码来尝试接收 FULFILLMENTS_UPDATE webhook,但我收到了一个错误。我不确定,但我认为这可能是一个错误。

是否可以使用 Shopify.Webhooks.Registry.register 注册其他 webhook?

        const response3 = await Shopify.Webhooks.Registry.register({
          shop,
          accessToken,
          path: "/webhooks",
          topic: "FULFILLMENTS_UPDATE",
          webhookHandler: async (topic, shop, body) =>{
            console.log("FULFILLMENT_UPDATE webhooks", body);
//            delete ACTIVE_SHOPIFY_SHOPS[shop]
          },
        });

        if (!response3.success) {
          console.log(
            `Failed to register APP_UNINSTALLED webhook: ${response.result}`
          );
        }
┃   InternalServerError: Cannot read property 'webhookSubscriptions' of undefined
┃       at Object.throw (/home/user/src/user_test_app/node_modules/koa/lib/context.js:97:11)
┃       at /home/user/src/user_test_app/node_modules/@shopify/koa-shopify-auth/dist/src/auth/index.js:100:42
┃       at step (/home/user/src/user_test_app/node_modules/tslib/tslib.js:133:27)
┃       at Object.throw (/home/user/src/user_test_app/node_modules/tslib/tslib.js:114:57)
┃       at rejected (/home/user/src/user_test_app/node_modules/tslib/tslib.js:105:69)
┃       at processTicksAndRejections (node:internal/process/task_queues:93:5)



请确保您在请求的应用范围中添加了 read_fulfillments(和 write_fulfillments,如果需要的话)。

您也可以尝试在注册中提供 apiVersion,但不确定在这种情况下是否有实际影响。

const registration = await Shopify.Webhooks.Registry.register({
    shop,
    accessToken,
    path: '/webhooks',
    topic: 'FULFILLMENTS_UPDATE',
    apiVersion: Shopify.Context.API_VERSION,
    webhookHandler: async (_topic, shop, _body) => {
        // ...
    },
})

我 运行 在尝试注册 PRODUCTS_CREATE webhook 主题时遇到了类似的问题。我将范围添加到请求的应用范围,但我仍然收到相同的 InternalServerError: Cannot read property 'webhookSubscriptions' of undefined.

对我有用的修复是在注册 webhook 主题时明确添加所需的范围:

const response = await Shopify.Webhooks.Registry.register({
  shop,
  accessToken,
  // THE FIX - Add the required scope here
  scope: 'read_products',
  path: "/webhooks/products/create",
  topic: "PRODUCTS_CREATE",
})

我有类似的东西: error while registering webhooks: TypeError: Cannot read properties of undefined (reading 'webhookSubscriptions') 我添加了我使用的所有范围,但没有修复它。

所以我尝试使用我有权添加的所有范围强制执行它,但仍然出现相同的错误。

原来是其中一个 webhook 主题中的拼写错误(INVENTORY_ITEM_CREATE 而不是 INVENTORY_ITEMS_CREATE)所以如果接受的解决方案不能解决您的问题我建议仔细检查所有主题名称,因为它给出了类似的错误。

TL;DR: 仔细检查您的主题名称