Problem Querying REST API from React + Node

Solved
Shopify Partner
20 2 6

@Ireneludi  ahh, did you install the koa router npm package?

 

npm install --save koa-router

If not, stop your server, run the above command, and start your server again.

0 Likes
Tourist
9 0 1

@Thresher Yes, I installed at very first.

QAQ I have been stuck with this API problem for the whole week....

0 Likes
Tourist
9 0 1

Finally figure it out. The issue was related with chrome security, after I disable chrome cors, everything works fine.

1 Like
Shopify Partner
25 0 3

Hello @Thresher, isn't a risky approach to store the accessToken into a cookie ? 

Wouldn't be better to: 
1) verify request is coming from your domain and avoid extra jobs if not 

2) get the stored access token from your DB using the shop name 

3) make the Shopify request 

4) return data 

 

cheers

 

0 Likes
Shopify Partner
20 2 6

Yes @Massive , that's what I'm doing now!

0 Likes
Tourist
21 0 0

I don't think disabling cross origin security in the browser is the solution.

0 Likes
New Member
12 0 0

Sorry for digging an old thread but it has part of the solution I need, and it's in the exact same context, query the API from the backend.

const fetchUrl = 'https://getordertest.myshopify.com/api/orders.json';
		const method = "GET";
		fetch(fetchUrl, {method: method,})
		.then(response=>response.json()).then(json => console.log(json))

The code above, is calling the orders api, which is what I need. But I have no idea where to insert this snippet.
Should I wrap it in a server.use? or maybe in a router.get? How do I make it trigger?
Even if its just for a test as soon as the server launches would do for me to get moving. I'm stuck trying to understand how to call the APIs for a long time.

0 Likes
Highlighted
Tourist
9 0 1

I used in router.get(`insert the code snippet here`)
Basically what I do is, from client, call my backend, and in backend router, call Shopify API

0 Likes
Tourist
21 0 0

Your best bet is to use the shopify-api-node package so that proper headers are sent for auth without having to manually configure them. All of this should happen on your server and not on the client so as not to expose your accessToken.

0 Likes