【问题标题】:Webhooks in shopify/shopify-api using nodejs使用 nodejs 在 shopify/shopify-api 中的 Webhook
【发布时间】:2021-03-09 01:06:27
【问题描述】:

我正在测试节点 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)



【问题讨论】:

标签: node.js shopify webhooks shopify-app shopify-api


【解决方案1】:

请确保您在请求的应用范围内添加了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) => {
        // ...
    },
})

【讨论】:

  • 我实际上忘了添加 read_fulfillments 范围。我做到了,它奏效了。谢谢!
【解决方案2】:

我在尝试注册 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",
})

【讨论】:

    猜你喜欢
    • 2016-05-12
    • 1970-01-01
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多