Fetch params + body being overwritten?

Solved
Highlighted
Tourist
5 1 1

 Hi,

I am having some trouble with calling the admin API from my app Node server.js file. I am using fetch on the front end component to access a route on my server to then trigger the admin API call from my back end, but am seeing unexpected results.

For reference, this is my code for the route:

router.get("/api/tag", verifyRequest(), async (ctx) => {
        try {
            console.log(HOST + ctx.query.src)
            const results = await fetch("https://" + ctx.cookies.get('shopOrigin') + "/admin/api/2020-04/script_tags.json?src=" + HOST + ctx.query.src, {
                headers: {
                    "X-Shopify-Access-Token": tokens[ctx.cookies.get('shopOrigin')]
                }
            })
            .then(response => response.json())
            .then(json => json)
            ctx.res.statusCode = 200;
            ctx.body = {
                status: 'success',
                data: results
            }
        } catch (err) {
            console.log(err)
        }
    })

I am also using koa-bodyparser middleware to parse the request body. Simply server.use(koabody()).

The problem I am getting is that if I console.log HOST + ctx.query.src I am correctly seeing my app URL appended by the src query parameter. However, when I trigger the fetch from my component in the browser, the request src parameter is simply the value of ctx.query.src, without the preceding HOST. It's almost as though this parameter is being overwritten in the fetch() inside my route.

The same is happening when I make POST calls. Whatever body I use in the fetch() in my route is overwritten by the fetch() body from the calling component.

Fetch and Koa are new to me. I was just hoping to extend the app that I built in the Shopify Node/React tutorial. I have Googled to no avail.

 

Any help appreciated.

Thanks

0 Likes
Highlighted
Tourist
5 1 1

This is an accepted solution.

This should be a warning to anyone tempted to log on before having a coffee...

Of COURSE I was seeing those parameters in the browser... it was showing the url I had told my component to fetch! I was looking in the wrong place for debugging - the fetch I was interested in was being called from my route callback, so I should have been looking at my console on the server. Still getting used to the React/Node way of working, evidently.

0 Likes