I have a code I was using inside Google GAS but I had to refactore it a bit to use it outside and I'm having trouble with a for loop I had to form an array. I'll make the code reproducible with read API Keys in a dev envirorment.
const optionsGet = {
'method': 'GET',
'contentType': 'application/x-www-form-urlencoded;charset=UTF-8',
'muteHttpExceptions': true,
};
const website = 'https://atopems-desarrollo.com.ar';
const ck = 'ck_112e7b278d5731d2cdb44a57c07bbeab2fc9196a';
const cs = 'cs_e891f6cd193608f47f8acf340b985317448cdca4';
var container = [];
var surl = website + '/wp-json/wc/v3/products?consumer_key=' + ck + '&consumer_secret=' + cs + '&per_page=20' + '&orderby=id' + '&order=asc' + '&status=publish' + '&page=20';
var url = surl
console.log(url)
var fetchCall = fetch(url, optionsGet)
.then(response => {
if (response.ok) {
return response.json()
} else {
throw new Error('beep-boop no dio 200 OK' + response.status);
}
console.log(Error);
})
var total_pages = 40;
var pages_count = 20;
while (pages_count < total_pages) {
pages_count++;
fetchCall
.then(data => {
for (var i = 0; i < data.length; i++) {
//console.log(i);
container.push({
sku: data[i]['sku'],
id: data[i]['id'],
price: data[i]['price']
});
}
})
console.log(container);
var surl = website + '/wp-json/wc/v3/products?consumer_key=' + ck + '&consumer_secret=' + cs + '&per_page=20' + '&orderby=id' + '&order=asc' + '&status=publish' + '&page=' + (pages_count + 1);
var url = surl
console.log(url);
var fetchCall = fetch(url, optionsGet)
.then(response => {
if (response.ok) {
return response.json()
} else {
throw new Error('beep-boop no dio 200 OK' + response.status);
}
console.log(Error);
})
}
The code right now is in the snippet its functional. I can't figure out how to use fetch and get the data inside the while and to push with a for loop to an empty array. The following snippet is how I think it should work?
const optionsGet = {
'method': 'GET',
'contentType': 'application/x-www-form-urlencoded;charset=UTF-8',
'muteHttpExceptions': true,
};
const website = 'https://atopems-desarrollo.com.ar';
const ck = 'ck_112e7b278d5731d2cdb44a57c07bbeab2fc9196a';
const cs = 'cs_e891f6cd193608f47f8acf340b985317448cdca4';
var container = [];
var surl = website + '/wp-json/wc/v3/products?consumer_key=' + ck + '&consumer_secret=' + cs + '&per_page=20' + '&orderby=id' + '&order=asc' + '&status=publish' + '&page=20';
var url = surl
console.log(url)
var fetchCall = fetch(url, optionsGet)
.then(response => {
if (response.ok) {
return response.json()
} else {
throw new Error('beep-boop no dio 200 OK' + response.status);
}
console.log(Error);
})
var preVal = [];
var total_pages = 40;
var pages_count = 20;
while (pages_count < total_pages) {
pages_count++;
fetchCall
.then(data => {
preVal.push(...data);
})
for (var i = 0; i < preVal.length; i++) {
//console.log(i);
container.push({
sku: preVal[i]['sku'],
id: preVal[i]['id'],
price: preVal[i]['price']
});
}
console.log(container);
var surl = website + '/wp-json/wc/v3/products?consumer_key=' + ck + '&consumer_secret=' + cs + '&per_page=20' + '&orderby=id' + '&order=asc' + '&status=publish' + '&page=' + (pages_count + 1);
var url = surl
console.log(url);
var fetchCall = fetch(url, optionsGet)
.then(response => {
if (response.ok) {
return response.json()
} else {
throw new Error('beep-boop no dio 200 OK' + response.status);
}
console.log(Error);
})
}
But preVal would be empty since it would be filled by the time I try to do the for loop.
I'm pretty new to JS and programming in general. So far I've read some stuff about promises but I can't figure it out.
What I need is the for loop to work in creating a new array, since the rest of the code wants to use only those 3 values from the WC response.
Thank you.