Y4TO
June 16, 2022, 1:02pm
1
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?
Y4TO
June 16, 2022, 1:26pm
3
I’m not sure but I think it’s set to offline session tokens as i get offline session tokens in the cookies
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.
Y4TO
June 16, 2022, 5:53pm
5
Thank you so much for the help! I am storing the session now.