ctx.session.shop is undefined

Highlighted
New Member
2 0 0

I am trying to implement REST api and on my server.js file when I try to fetch url using ctx.session.shop, I am getting request to https://undefined/admin/api/2020-10/products.json failed, reason: getaddrinfo ENOTFOUND undefined'. Can't figure out why this is happening, I cleared cache and tried reinstalling app but still it's undefined. 

Also, If you can take a look and tell me if anything is wrong with the way I am implementing it, that would be very helpful...

 

router.get('/get/products', async (ctx, next) => {
    async function getP() {
      try {
        var url = `https://${ctx.session.shop}/admin/api/2020-10/products.json`;
        await fetch(url, {
          method: 'GET',
          json: true,
          headers: {
            'X-Shopify-Access-Token': ctx.session.accessToken,
            'content-type': 'application/json'
          }
        }).then(res => {
          res.json();
          console.log(res);
        }).catch(error => { console.log(error) })        
       next();
      }
      catch (err) {
        console.log(err);
      }
    }
    getP();
  })

 

0 Likes
Highlighted
Shopify Partner
44 0 9

Hi @ashish112 ,

This category is more focused for merchants finding apps to solve their problems, but I'll try to answer your question.

It looks like you're using the Shopify Koa package to handle OAuth during installation, which means that the shop name (https://<shop name here>.myshopify.com) is stored as a cookie.

So in order to access the current logged in shop in your API route, use `ctx.cookies` to find the cookie that contains the shop's name.

If you need further help, I recommend posting to the Developer's category in the forum, or asking on the official Shopify Partner Slack.

You shouldn't have to think about fraud on your Shopify store.
Ask me about
0 Likes