Development discussions around Shopify APIs
This is the repository in question
The app writes the data we need into data.json (in the root directory) upon install, but I need the data.json to be overwritten on demand (i.e. onClick event). Is that even possible?
I have tried transposing the GET request function from server.js into a page (index.js) file, but upon compiling the page and pressing the button with the bound function, I get these errors in the console, pertaining to CORS policies. I don't know how the initial GET request upon install in server.js bypassed CORS, but I need to do the same thing in index.js.
server.js
server.use(
createShopifyAuth({
apiKey: SHOPIFY_API_KEY,
secret: SHOPIFY_API_SECRET_KEY,
scopes: ['read_products', 'write_products', 'read_orders', 'write_orders'],
async afterAuth(ctx) {
const { shop, accessToken } = ctx.session;
ctx.cookies.set('shopOrigin', shop, { httpOnly: false });
ctx.cookies.set('accessToken', accessToken, {httpOnly: false})
const options = {
method: 'GET',
credentials: 'include',
headers: {
'X-Shopify-Access-Token': accessToken,
'Content-Type': 'application/json',
},
};
await fetch(`https://${shop}/admin/orders.json?status=any`, options)
.then((response) => response.json())
.then((data) => {
fs.writeFile('./data', JSON.stringify(data, null, 4), (error) => {
if (error) {
console.error(error);
return;
}
console.log('File has been created');
});
})
.catch((error) => console.error( error));
ctx.redirect('/');
},
}),
);
index.js
async rewrite(){
console.log("rewrite");
const options = {
method: 'GET',
credentials: 'include',
headers: {
'X-Shopify-Access-Token': cookies.get('accessToken'),
'Content-Type': 'application/json',
},
};
await fetch(`https://${cookies.get('shopOrigin')}/admin/orders.json?status=any`, options)
.then((response) => response.json())
.then((data) => {
fs.writeFile('./data', JSON.stringify(data, null, 4), (error) => {
if (error) {
console.error(error);
return;
}
console.log('File has been created');
});
})
.catch((error) => console.error( error));
}
I know this is an old post but did you manage to work it out?
Thanks for replying and yes it did help and I did manage to get mine working but now faced with another issue aha
User | RANK |
---|---|
42 | |
16 | |
9 | |
9 | |
8 |
Thanks to all Community members that participated in our inaugural 2 week AMA on the new E...
By Jacqui Mar 10, 2023Upskill and stand out with the new Shopify Foundations Certification program
By SarahF_Shopify Mar 6, 2023One of the key components to running a successful online business is having clear and co...
By Ollie Mar 6, 2023