2014-12-06 108 views
0

我有点新异步功能和种类的困惑如何通过在async.each回调每个项目下一级(中间件)异步在节点快速

逻辑:我想通过所有项目,如果项目是“type2”,我需要通过emailExists来检查电子邮件是否存在!在处理完所有项目后,我们才会转到下一个中​​间件,前提是没有类型2的项目或存在任何电子邮件!这里我提到了我的sudocode;我真的很感激,如果你帮我用异步功能,我很新,请让我知道我做错了什么:

请注意在第二个块总是错误和数据是不确定的?!这是为什么?即使它将发送到emailExistence.check!

请让我知道如果你需要更多的澄清!感谢:-)

async.each(Object.keys(items), function (item, callback) { 


     if (!req.body[item] && items[item].type) { 
      // assiging the default values based on the default type      
      switch (items[item].type1) { 
       case 'name1': 
        //. 
        //.No callback functions here 
        //. 
        callback(); 
        break; 
       case 'name2': 
        //. 
        //.No callback functions here 
        //. 
        callback(); 
        break; 
       case 'name3': 
        //. 
        //.No callback functions here 
        //. 
        callback(); 
        break; 
       case 'name4': 
        //. 
        //.No callback functions here 
        //. 
        callback(); 
        break; 
       default: 
        //. 
        //.No callback functions here 
        //. 
        callback(); 
        break; 
      } 
     } else if (items[item].type2) { 
      emailExistence.check(req.body[item], function (err, exists) { 

       if (err) { 
        callback(err); 
       } else {      
        callback(null, exists); 
       } 
      }); 

     } else { 
      callback(); 
     } 
    }, 
      function (err, data) { 
       // Here always err and data are undefined?! Why is that? Even though it is going to emailExistence.check! 
       if (err) { 
        res.json(400, err); 
       } else { 
        if (data) { 
         next(); 
        } else { 
         res.json(400, {"data": "email not exist"}); 
        }else{ 
         next(); 
        } 
       } 
     } 
) 

被更新(响应阿龙):

亚伦你是对的,async.each回报只是一个parametere(ERR);但如果我们把它放在循环里面,地图版本不会工作!你可以帮我吗;是这个版本你正在与地图:

function middleware(req, res, next) { 
    for (var item in items) { 


     if (!req.body[item] && items[item].type) { 
      // assiging the default values based on the default type      
      switch (items[item].type1) { 
       case 'name1': 
        //. 
        //.No callback functions here 
        //. 
        callback(); 
        break; 
       case 'name2': 
        //. 
        //.No callback functions here 
        //. 
        callback(); 
        break; 
       case 'name3': 
        //. 
        //.No callback functions here 
        //. 
        callback(); 
        break; 
       case 'name4': 
        //. 
        //.No callback functions here 
        //. 
        callback(); 
        break; 
       default: 
        //. 
        //.No callback functions here 
        //. 
        callback(); 
        break; 
      } 
     } else if (items[item].type2) { 
      var emails = []; 
      emails.push(req.body[field]); 
      // In my case here there is just one email (array of one item) 
      async.map(emails, checkEmail, function (err, result) { 
       if (!results) { 
        res.json(400, {"msg": "email does not exist"}); 
       } 

      }); 

     } 

    } 
    next(); 


    function checkEmail(email, callback) { 

     emailExistence.check(email, function (err, exists) { 

      if (err) { 

       callback(err); 
      } else { 

       callback(null, exists); 
      } 
     }); 
    } 
} 

回答

-1

async.each仅用于副作用。正如文档所述,传递给迭代器的回调仅仅需要参数,最终的回调只能得到一个参数 - err。您可能需要async.map,期望errtransformed在传递给迭代器的回调中,并将errresults传递给最终回调。

+0

谢谢你!你是绝对正确的!但仍然映射在一个for循环是不会工作,也许我不明白你的解决方案!我刚刚更新了我的问题,从我的建议中得到了什么!你能帮我解决吗? – user385729 2014-12-07 00:30:22

+1

@Pasargad在你的例子中,用'async.map'代替'async.each'。不需要for循环。 – 2014-12-07 01:11:06

+0

非常感谢老兄!我很感激 :-) – user385729 2014-12-07 01:32:55