0

I'm facing an issue, using Product resource on Shopify REST Api : I need to display many products on a Node.js Express app, I've left the default limit at 50 and when I want to fetch the next page of 50 products when I click on a next or previous button (I use the nextPageUrl/prevPageUrl from the response headers) I get a 401 error if the request is made from the client-side because of CORS error

Then I tried to make the request on server-side, I've passed the link from client to server when hitting the next button for example but it still does not work

The documentation is not clear at all about the paginated request and nothing that i've done from the documentation now is correct, it just says "Make a GET request to the link headers" and voila

Anyone have done this before ? Code below

protectedRouter.get('/inventory', async (req, res) => {
  try {
    const session = await Shopify.Utils.loadCurrentSession(req, res);
    const client = new Shopify.Clients.Rest(session.shop, session.accessToken)

    const result = await client.get({
      path: 'products',
    })

    res.render('inventory', {
      products: result,
    })
  } catch (error) {
    throw error;
  }
});
  script(src="/scripts/pagination.js")
  div.View
    h3.title.my-4 Etat des stocks
    div.row
      div.col-6
        span(id='previousLink') #{products.pageInfo.prevPageUrl ? products.pageInfo.prevPageUrl : '' }
        button.btn.btn-outline-dark.ml-4(id="previous_btn")
          i(class="bi bi-arrow-left-circle-fill") Précédent
      div.col-6
        span(id='nextLink') #{products.pageInfo.nextPageUrl ? products.pageInfo.nextPageUrl : '' }
        a.btn.btn-outline-dark.float-right.mr-4(id="next_btn") Suivant 
          i(class="fa-solid fa-circle-arrow-right")
window.addEventListener('DOMContentLoaded', () => {
    console.log('dom content loaded')

    document.getElementById('next_btn').onclick = function (e) {
        console.log('next button clicked')
        const nextLink = document.getElementById('nextLink').innerHTML;

        fetch(nextLink).then(response => {
            console.log(response);
        }).catch(error => {
            console.log(error)
        })

        console.log(nextLink)
    }
});

1 Answer 1

0

You're doing it wrong. If you want to display Shopify products in your own App, you use the StorefrontAPI calls. With a StorefrontAPI token, you get products, and can display them. Trying to use Admin API calls is never going to work properly. Switch to StorefrontAPI and all your problems go away.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.