Johnathan David
Johnathan David

Reputation: 80

Webhooks in shopify/shopify-api using nodejs

I was testing the node shopify-api and I noticed that there was a code in server.js that registers the APP_UNINSTALLED webhook. so, I added the code below to try to receive the FULFILLMENTS_UPDATE webhook but I am getting an error. I am not sure but I am thinking that this might be a bug.

Is it possible to register other webhooks using Shopify.Webhooks.Registry.register?

        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)



Upvotes: 1

Views: 4253

Answers (3)

Forgotten-Storm
Forgotten-Storm

Reputation: 307

I had the something similar: error while registering webhooks: TypeError: Cannot read properties of undefined (reading 'webhookSubscriptions') I added all the scopes that I used it did not fix it.

So I tried to brute forced it with all scopes that I had permission to add and still got the same error.

Turned out to be a spelling mistake in one of the webhook topics (INVENTORY_ITEM_CREATE instead of INVENTORY_ITEMS_CREATE) so if the accepted solution does not fix your problem I'd recommend double checking all the topic names because it gives similar errors.

TL;DR: Double check your topic names

Upvotes: 0

Ryan Donald Liszewski
Ryan Donald Liszewski

Reputation: 13

I ran into a similar issue trying to register the PRODUCTS_CREATE webhook topic. I added the scopes to the requested app scopes but I still received the same InternalServerError: Cannot read property 'webhookSubscriptions' of undefined.

The fix that worked for me was to explicitly add the required scope when registering the webhook topic:

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",
})

Upvotes: 0

Aurélien B.
Aurélien B.

Reputation: 517

Please make sure you added the read_fulfillments (and write_fulfillmentsif needed) in your requested app scopes.

Also you can try to provide an apiVersion inside your registration, but not sure if it has a real impact in this case.

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

Upvotes: 2

Related Questions