2017-04-01 3 views
0

Ich möchte mehrere Seiten mit request in async.forEach anfordern. Ich habe mehrere links, und ich möchte zu jedem Link gehen und die sublinks daraus bekommen. Dann möchte ich jeden Sublink bewerten und die Ergebnisse an dataarray senden. Aber nur die erste sublink wird mehrfach ausgewertet, für jede links Array-Element. Aber ich möchte jede sublink bewerten. Hier ist mein Code:Anforderung wird nicht korrekt in async.forEach ausgewertet

var dataarray = []; 
var links = ["www.mysite.com/1","www.mysite.com/2","www.mysite.com/3"]; 

async.whilst(
    function() { console.log("Number of links left: " + links.length); return links.length > 0; }, 
    function(innerCallback){ 
     var i1 = getRandomInt(1, links.length); //getRandomInt is a function that gives a random integer in array. 
     var Theurl = links[i1-1]; 
     request({ 
      url: Theurl, 
      //More paramaters to request here... 
     }, function(error, response, body) {   
      if (response) { 
       //Evaluate some jquery to get a variable allsublinks used later on. 
       //So I get several sub-links in each element in links array. 
       var allsublinks = stuff; 

       //Now I define a function to be used in async.forEach. I basically need to 
       //loop over allsublinks array that contains different url links. 
       function getInfo(name, thiscallback) { 
        console.log("Online"); 
        console.log("Current url: "+name);   
        request({ 
        url: name, 
        //Other parameters to request. 
        }, function(error, response, body) { 
         if (response) { 
         //Here I evaluate some jquery again to get the datafromthislink variable 
         dataarray.push(datafromthislink); 
         setTimeout(function() { thiscallback(); });               
        } 
        }) 
       } 

       //Now I use async.forEach to loop over allstorelinks. 
       async.forEach(allsublinks, getInfo, function(err, results) { 
        console.log("dataarray: ",dataarray); 
        setTimeout(function() { links.splice(i1-1, 1); innerCallback(); }); 
       }); 
      } 
     }) 
    }, 
    function(err){ 
     console.log("All Done"); 
}) 

Was mache ich falsch?

Mit freundlichen Grüßen

+0

Warum verwenden Sie während für Links? 'async.each (Links, Funktion (Link, Callback) {' hätte Ihnen eine Menge Ärger erspart. Nicht sicher, aber wahrscheinlich ist das Problem irgendwo im while Teil. –

+0

Verwenden Sie die Funktion zum Schließen –

+0

@AlokDeshwal Bitte schreiben Sie eine Antwort? :) – user1665355

Antwort

0

Dies könnte die sauberere Version von dem, was Sie erreichen wollen, die Ihr Konzept klar von Rückrufe richtig verwenden.

var dataarray = []; 
var links = ["www.mysite.com/1", "www.mysite.com/2", "www.mysite.com/3"]; 

//Now I define a function to be used in async.forEach. I basically need to 
//loop over allsublinks array that contains different url links. 
function getInfo(link, infoCallback) { 
    console.log("Online"); 
    console.log("Current url: " + link); 
    request({ 
     url: link, 
     //Other parameters to request. 
    }, function (error, response, body) { 
     if (error) console.log(error); 
     if (response) { 
      //Here I evaluate some jquery again to get the datafromthislink variable 
      dataarray.push(body.datafromthislink); 
     } 
     infoCallback(); 
    }) 
} 

function getLink(link, linkCallback) { 
    console.log('checking link: ' + link); 
    request({ 
     url: link, 
     //More paramaters to request here... 
    }, function (error, response, body) { 
     if (error) console.log(error); 
     if (response) { 
      //Evaluate some jquery to get a variable allsublinks used later on. 
      //So I get several sub-links in each element in links array. 
      var allsublinks = body.stuff; 
      //Now I use async.forEach to loop over allstorelinks. 
      async.forEach(allsublinks, getInfo, linkCallback); 
     } else { 
      linkCallback(); 
     } 
    }) 
} 

async.each(links, getLink, function (err) { 
    if (error) console.log(error); 
    console.log("All Done"); 
    console.log(dataarray); 
});