2016-09-30 182 views
-2

我對異步瀑布有點問題,因爲我想在循環處理的函數之後調用下一個函數。腳本是異步瀑布流的一部分,以前的函數工作得很好。這裏唯一的事情是,它有一個用於loop.The代碼below.Any幫助將是非常appreciated.Thanks節點js異步回調問題

function(querySize,req) 
{ 
console.log("in second function call :" +querySize); 
var temp,title; 
for(var counter = 0;counter <= querySize;counter++) 
{ 
    websiteAddressArray[counter] = req.query.address[counter]; 
    console.log("for is : " +req.query.address[counter]); 
    console.log("first line is : " +websiteAddressArray[counter]); 
    console.log("second line is : " +websiteAddressArray[counter]); 
    request(websiteAddressArray[counter], function(err, resp, body) 
    { 
     $ = cheerio.load(body); 
     console.log("title is : " +$('title').text()); 
     titleArray[counter] = $('title').text(); 
    }); 
    } 
    var line="this is string"; 
    callback(null,line); 
}, 
function(callback,line) 
{ 
    console.log("line is : " +line) 
}, 
+1

你的回撥在哪裏?它是瀑布代碼的一半,你可以發佈你的async.waterfall代碼嗎? – abdulbarik

+0

@abdulbarik我編輯了代碼。我的回調是下一個功能,它採取行,並簡單地打印它 –

+0

什麼是控制檯打印'console.log(「行是:」+行)'? – abdulbarik

回答

1

syntax是不正確的。總是通過callback最後一個參數async.waterfall

function(querySize, req,callback) { 
    console.log("in second function call :" + querySize); 
    var temp, title; 
    for (var counter = 0; counter <= querySize; counter++) { 
    websiteAddressArray[counter] = req.query.address[counter]; 
    console.log("for is : " + req.query.address[counter]); 
    console.log("first line is : " + websiteAddressArray[counter]); 
    console.log("second line is : " + websiteAddressArray[counter]); 
    request(websiteAddressArray[counter], function(err, resp, body) { 
     $ = cheerio.load(body); 
     console.log("title is : " + $('title').text()); 
     titleArray[counter] = $('title').text(); 
    }); 
    } 
    var line = "this is string"; 
    callback(null, line); 
}, 
function(callback, line) { 
    console.log("line is : " + line) 
}, 
+0

bro我已經改變了你說的語法,但錯誤仍然是相同的「回調未定義」 –

+0

是的,因爲你沒有定義回撥以上 – abdulbarik

+0

看到我更新的答案它應該工作 – abdulbarik