Hi i am creating a shopify api node js script which runs with async and await.Basically i am using this to fetch data form paginated page and i am getting the data properly in console log for each page. the issue is i am unable to make a array in which i get all the data in the end of function.
Here is the code
const fetch = require("node-fetch");
const priceRule = "641166639179"
const totalresult = []
async function findCodeId(price_rule, url=null){
//get(priceRuleId, id)
let urlneww = url ? url : `https://xxxxxxxxxxxx.myshopify.com/admin/api/2020-01/price_rules/${price_rule}/discount_codes.json`;
await fetch(urlneww, {
method: 'GET',
headers: {
"Content-Type": "application/json",
"Authorization": "Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx="
}
})
//.then(response => response.json())
.then(result => {
let rrr = result.json() ;
rrr.then((data)=>{
totalresult.push(data)
console.log(data)
}).catch(error => error);
if(result.headers.get('link').includes("next")){
let str = result.headers.get('link');
let arrStr = str.split('<').pop().split('>')[0]; // returns 'two'
//console.log(arrStr);
findCodeId(priceRule,arrStr);
//console.log(totalresult)
}else{
}
//return totalresult
})
.catch(error => console.log('error', error));
}
findCodeId(priceRule)
i am trying to push the data in totalresult constant but it is not working. Could you please suggest how can i do this so that on each result it pushes the data in the totalresult and at end of function i got all result data collected in totalresult.
response = await fetchthendata = await response.json(). to catch errors, put the await statements in try catch block.