0
module.exports= {
getHomePost:function(req,res) {
post_model.getHomePosts('1',function(err,rows) {
  var locals={}; 
  async.parallel([
    function(callback) {
      for(var i=0;i<rows.length-1;i++) {
        async.series([ 
          function(call){
            post_model.getPostDetails(rows[i].Post_Date_Time_id,rows[i].Post_Personid_id,function(err,row) {
              locals.rows[]=row;
              call();
           });
          }  
        ],function(err){ 

        //console.log(locals.rows);
        });
      } 
      callback();
    }
  ],function(err) {
    console.log(locals.rows);
    res.render('home',locals);
  });
});
}
} 

“我想在从 for 循环获取输出后执行渲染。但我无法做到。请帮助。在从循环获取值之前,我的回调函数执行。但我想在从 for 循环获取输出后执行它"

4

1 回答 1

0

这可能会帮助你......

  module.exports= 
    {
        getHomePost:function(req,res) 
        {
            post_model.getHomePosts('1',function(err,rows) 
            {
                var locals={}; 
                async.parallel([
                function(callback) 
                {
                    var count = 0;
                    for(var i=0;i<rows.length-1;i++) 
                    {
                        async.series([ 
                        function(call)
                        {
                            post_model.getPostDetails(rows[i].Post_Date_Time_id,rows[i].Post_Personid_id,function(err,row) 
                            {
                                locals.rows[]=row;
                                call();
                            });
                        }],
                        function(err)
                        { 
                            //console.log(locals.rows);
                        });
                        count++;
                    } 
                    if(count == rows.length)
                    {
                        callback();
                    }
                }],
                function(err) 
                {
                    console.log(locals.rows);
                    res.render('home',locals);
                });
            });
        }
    } 
于 2013-09-18T11:07:17.353 回答