nextjs-shopify-app-no-custom-server icon indicating copy to clipboard operation
nextjs-shopify-app-no-custom-server copied to clipboard

Error while trying to pull products

Open ikn-n opened this issue 3 years ago • 8 comments
trafficstars

On a local env, it runs into trouble when it tries to fetch products. It appears that it's not able to get an online session

event - compiled client and server successfully in 438 ms (1421 modules)
error - (api)/pages/api/products.js (10:53) @ __WEBPACK_DEFAULT_EXPORT__
TypeError: Cannot read properties of undefined (reading 'shop')
   8 | 
   9 |   const session = await Shopify.Utils.loadCurrentSession(req, res);
> 10 |   const client = new Shopify.Clients.Graphql(session.shop, session.accessToken);

ikn-n avatar Feb 11 '22 22:02 ikn-n

Can you console.log the session

redochka avatar Feb 12 '22 10:02 redochka

The session is undefined after line 9. I followed it to where It tries to look it up in the store, but the store has only one key for this shop, the offline__shopname_ key so it fails to "load" a session and returns undefined.

If I change line 9 to have Shopify.Utils.loadCurrentSession(req, res, false); then it works, but I want to see it working with online sessions as well

ikn-n avatar Feb 12 '22 15:02 ikn-n

In pages/api/auth/[...shopify].js#5, pass 'online' instead of 'offline'

accessMode: 'offline',  //offline vs online

redochka avatar Feb 12 '22 15:02 redochka

Thanks for that. In the example above, it only ever issues an online token, what will happen when it expires? It will try to load it from the DB, probably won't find one and will fail again. How would it handle the reissuing of new online tokens and provide an offline one for API access?

ikn-n avatar Feb 16 '22 20:02 ikn-n

It would be great if you add support for both online and offline tokens in the same app. It's unfortunate that the current version of this app always breaks down out of the box on its first load.

Screenshot from 2022-02-22 21-28-27

ikn-n avatar Feb 22 '22 21:02 ikn-n

Hey guys. Thanks for this boilerplate @redochka - it's awesome and definitely much better than the shopify-cli generated one. 👊

I also have this issue of online and offline sessions. I am building a fully standalone, offline app but would be nice to also be able to deliver content with @shopify/polaris in the store -> app iframe window. Is there a way we can adapt it to have both an online and offline sessions when needed?

seanonthenet avatar Mar 16 '22 10:03 seanonthenet

It seems the trick would be to authorise the user twice? https://rsirokov.medium.com/shopify-app-online-and-offline-access-modes-8a8c5ecd928b Based more on the shopify-cli version, but I'm sure it could be done somewhere here too... https://github.com/redochka/nextjs-shopify-app-no-custom-server/blob/main/lib/shopify.js

TheApeMachine avatar Apr 07 '22 22:04 TheApeMachine