See the comments in code to see what's actually happening/not happening. Basically within the function, the array.push() method does not seem to work and I cannot wrap my head around why. Any help would be appreciated.
var locationArray = new Array;
request(process.env.RESOURCE_SHEET, (error, response, html) => {
if(!error && response.statusCode == 200) {
const $ = cheerio.load(html);
$("h3").each((i, lle) => {
const location = $(lle).text();
if(location.includes("Kansas")) return;
if(location.includes("In Stock")) {
var level = location + " ✅";
} else {
var level = location + " ❌";
}
locationArray.push(level); // This doesn't push to the array
});
}
});
locationArray.push("test") // This pushes to the array
console.log(locationArray) // Output: "test"
Thanks to Harsh Saini.
His response: request works asynchronously, you should try console the array inside the request callback.
Working code:
var locationArray = new Array;
request(process.env.RESOURCE_SHEET, (error, response, html) => {
if(!error && response.statusCode == 200) {
const $ = cheerio.load(html);
$("h3").each((i, lle) => {
const location = $(lle).text();
if(location.includes("Kansas")) return;
if(location.includes("In Stock")) {
var level = location + " ✅";
} else {
var level = location + " ❌";
}
locationArray.push(level);
});
}
console.log(locationArray)
});