I am using Request-Promise and cheerio to scrape some website data, essentially I am trying to achieve the following:
- Create An Empty Array
- Login
- Get some info from one page and push objects into the array
- Get some info from another page and push objects into the array
- For each now object in the array, I need to:
- Go to the URL stored within that object {link: "some url", items: []}
- loop through all the items found within that link, and push this to the items array within the iterated object as such: {link: "some url", items: [{item},{item}]}.
- Access the completed orderArray, which should spit out something like this:
{link: "some url", items: [{item},{item}]}, {link: "some url", items: [{item},{item}]}, {link: "some url", items: [{item},{item}]}
Step 6 is where I'm running into issues, I don't know how to do this without nesting the promise inside a for loop as per my code below which then starts getting nasty. Can I be pointed in the right direction here?
Here is my current code:
let orderArray = []; rp.post(login) .then(function(res1){ // Login & Set Cookies cookieJar = res1.headers['set-cookie']; return rp(getOpenOrders); }) .then(function($){ // Get Some Info from getOpenOrders orderArray.push({info}); return rp(getShippedOrders); }) .then(function($){ // Get Some Info from getShippedOrders orderArray.push({info}); return orderArray; }) .then(function($){ // Loop through each object in the orderArray for (i = 0,; i < orderArray.length; i++){ rp(orderArray[I].link) .then(function($){ //Get length of items on page let itemsOnPage = $('tbody tr').length; //Get some more details for each object for (j = 0,; j < items.length; j++) { let moreinfo = {…}; orderArray.items.push(moreinfo); } } } return orderArray; }) .then(function($){ // Log finished Array console.log(orderArray); }) .catch(function(err){ console.log(err); }) };