Intereting Posts

新手async.waterfall

我试过使用asynchronous。 我有一个async.waterfall的路由function。 第一个函数调用外部函数并获取usersData中的所有用户第二个函数通过async.each调用外部函数为每个用户search信息。 我想再次传递用户数据与新闻价值第3function。 在第三个function,目前,我有一个async.each,我看每个用户的数据。 我的问题

1)在第二个函数中,我不会获取每个用户的信息。 2)第三个函数在第二个之前被调用,而我没有获取新的数据谢谢

router.post('/launch',function(req,res,next){ async.waterfall([ function(cb){ // fetch the global users fetchUsers(usersData,cb); }, function(usersData,cb){ async.each(usersData, function(userdata,cb){ // fetch other data for each user calcBalance(userdata, cb); },function(err){ cb(err,usersData); }); }, function(usersData,cb){ async.each(usersData, function(userdata,cb) { //watch the info with the news data console.log(' 2 '+ JSON.stringify(userdata)); //console.log(3); } ); }, ], function(err,results){ console.log('Fin' + JSON.stringify(results)); res.render('synchros',{launch:'end'},results); }); res.render('synchros',{launch:'end'}); }); function calcBalance(userData,cb){ var user_id=userData.id, resultCalcBalance=0, cats_id=[3,4,6], tabData={}; async.each(cats_id,function(cat_id,cb){ switch (cat_id) { case 3: var comp = "<="; break; case 4: var comp = "<="; break; case 6: var comp = "<"; break; }// fin du switch var myquery = "select blabla+ //console.log(calcul_balance); connectionMysql.query(myquery, function (err, rows, fields,cb) { if (err) { console.log('Error ' + err); cb(err); } else if (rows.length != 0) { if (rows != 0) { }// end if else { }// end else }); // end connectionMysql },function(err){ cb(err,userData); // ?? I send the data here }); cb(null, userData); // ?? I send the data here ?? } 

Solutions Collecting From Web of "新手async.waterfall"

我重新进行了修正,修正了一些错别字,并改变了callback的名字。 我改变了第二个async.each async.map,因为你正在处理一个数组,以获得每个项目的一组结果。

第一个问题是在倒数第二线。 您从calcBalance过早地回电。 另一个潜在的问题是第二个瀑布函数(以及calcBalance中的模糊callback名称cb )。

最后,你从来没有在第三个瀑布函数中运行async.eachcallback函数,如果你callback它,那是偶然的。

您还没有从一个数据库查询报告成功,所以如果它工作,你将需要调用done() 。 您可能还想使用async.map进行数据库调用,这可以让您组装结果,如done(null, balanceForCategory)

 router.post('/launch', function(req, res, next){ async.waterfall([ function(done){ // fetch the global users fetchUsers(usersData,done); }, function(usersData,done){ async.map(usersData, function(userdata, done2){ // fetch other data for each user calcBalance(userdata, done2); },function(err, results){ done(err,usersData); }); }, function(usersData, done){ async.each(usersData, function(userdata, done2) { //watch the info with the news data console.log(' 2 '+ JSON.stringify(userdata)); //console.log(3); }, done) }, ], function(err, results){ // results will be undefined because we called done() from the above async.each console.log('Fin' + JSON.stringify(results)); res.render('synchros', {launch:'end'}, results); }); // end of async.each }); // end of router.post() function calcBalance(userData, callback){ var user_id=userData.id, resultCalcBalance=0, cats_id=[3,4,6], tabData={}; async.each(cats_id, function(cat_id, done){ switch (cat_id) { case 3: var comp = "<="; break; case 4: var comp = "<="; break; case 6: var comp = "<"; break; }// fin du switch var myquery = "select blabla"; //console.log(calcul_balance); connectionMysql.query(myquery, function (err, rows, fields, queryCb) { // what is this queryCb param? if (err) { console.log('Error ' + err); queryCb(err); // This will callback whatever mySql passed in as queryCb // done(err) // This will callback out of the async.each iterator and immediately the async.each callback // callback(err) // This will callback out of calcBalance and continue executing // return callback(err); // This will callback out of calcBalance and stop executing } else if (rows.length != 0) { if (rows != 0) { // Your code might hang here without calling a callback } else { // Your code might hang here without calling a callback } }); // end connectionMysql },function(err){ // Inside async.each callback. Either everything worked or something broke callback(err,userData); // Send the data back out of calcBalance }); //callback(null, userData); // Running this here will IMMEDIATELY call back before async.each runs } 

calcBalance函数

 function calcBalance(userData,callback){ // Ensuite on va calculer les rtt_balances et holiday_balances et yesterday_extra_hours_month var user_id=userData.id, resultCalcBalance=0, cats_id=[3,4,6], tabData={}, dateJour=moment().format('YYYY-M-D');; async.each(cats_id,function(cat_id,done){ switch (cat_id) { case 3: var comp = "<="; break; case 4: var comp = "<="; break; case 6: var comp = "<"; break; }// fin du switch var calcul_balance = "select * from table1" connectionMysql.query(calcul_balance, function (err, rows, fields,queryCb) { if (err) { queryCb(err); // This will callback whatever mySql passed in as queryCb // done(err) // This will callback out of the async.each iterator and immediately the async.each callback // callback(err) // This will callback out of calcBalance and continue executing // return callback(err); // This will callback out of calcBalance and stop executing console.log('Error ' + err); queryCb(err); } else if (rows.length != 0) { if (rows != 0) { // On va chercher les valuers sinon on les laisse à zéro par défaut. for (var j = 0; j < rows.length; j++) { if (!isNaN(rows[j].amount) && rows[j].amount != null) { resultCalcBalance += parseInt(Math.round(rows[j].amount * 100) / 100); //console.log('ResultCalculBalance 1chiffre ' + parseInt(Math.round(rows[j].amount*100)/100) + ' 2chiffre' + resultCalcBalance); } else { resultCalcBalance += 0; //console.log('ResultCalculBalance 2' + JSON.stringify(rows[j].amount)); } } // fin du for k //console.log('Resultat : ' + userData.id + ' ' + cat_id + ' ' + resultCalcBalance); if (cat_id == 3) userData.holiday_balance = resultCalcBalance; if (cat_id == 4) userData.rtt_balance = resultCalcBalance; if (cat_id == 6) userData.yesterday_extra_hours_month = resultCalcBalance; }// fin du if else { if (cat_id == 3) userData.holiday_balance = 0; if (cat_id == 4) userData.rtt_balance = 0; if (cat_id == 6) userData.yesterday_extra_hours_month = 0; }// fin du else }// de la condition err ou pas console.log('1 '+JSON.stringify(userData)); }); },function(err){ callback(err,userData); }); //callback(null, userData); // Running this here will IMMEDIATELY call back before async.each runs