Why does my app crash upon receiving a webhook?


Hello,

My app keep crashing with this error whenever I receive a webhook.

client = new rest_1.RestClient(session.shop, session.accessToken);
                                                       ^

TypeError: Cannot read properties of undefined (reading 'shop')
    at Function.

I am responding with a 200 status response when i receive a webhook but it still crashes. It is really frustrating me as I am unable to find a solution.

This would happen if you do not store the offline session. Do you store it? Is your app set to online or offline session tokens? Or both?

I’m not sure but I think it’s set to offline session tokens as i get offline session tokens in the cookies

cookie.PNG

And I don’t think I’m storing the offline session.
this is how im initializing the context

const { API_KEY, API_SECRET_KEY, SCOPES, SHOP, HOST } = process.env
if (!API_KEY) {
    console.log(API_KEY);
}

Shopify.Context.initialize({
    API_KEY,
    API_SECRET_KEY,
    SCOPES: [SCOPES],
    HOST_NAME: HOST.replace(/https:\/\//, ""),
    IS_EMBEDDED_APP: false,
    API_VERSION: ApiVersion.April22 // all supported versions are available, as well as "unstable" and "unversioned"
});

and the only time im getting a session is on /auth/callback

case "/auth/callback":
            try {
                const session = await Shopify.Auth.validateAuthCallback(request, response, query as AuthQuery);
                ACTIVE_SHOPIFY_SHOPS[SHOP] = session.scope;

                const response = await Shopify.Webhooks.Registry.register({
                    path: '/webhooks',
                    topic: 'CUSTOMERS_CREATE',
                    accessToken: session.accessToken,
                    shop: session.shop,
                  });
              
                  if (!response['CUSTOMERS_CREATE'].success) {
                    console.log(
                      `Failed to register CUSTOMERS_CREATE webhook: ${response.result}`
                    );
                  }

                // all good, redirect to '/'
                const searchParams = new URLSearchParams(request.url);
                const host = searchParams.get("host");
                const shop = searchParams.get("shop");
                response.writeHead(302, { Location: `/?host=${host}&shop=${shop}` });
                response.end();
            }
            catch (e) {
                console.log(e);

                response.writeHead(500);
                if (e instanceof Shopify.Errors.ShopifyError) {
                    response.end(e.message);
                }
                else {
                    response.end(`Failed to complete OAuth process: ${e.message}`);
                }
            }
            break;
        // end of if (pathName === '/auth/callback'')

I tried storing the session but i’m kind of clueless how to do it.

I see. To know if you store it or not you need to check in the context if you are using custom storage ? (Check this line here https://github.com/Shopify/shopify-app-template-node/blob/a572a919cf26b1e913fe176e6605191c5a8bd024/server/index.js#L25)

Also are you setting your app online or offline (check this line here https://github.com/Shopify/shopify-app-template-node/blob/a572a919cf26b1e913fe176e6605191c5a8bd024/server/index.js#L11)

Thank you so much for the help! I am storing the session now.