async.each在设置之后无法设置标头

时间:2015-06-08 18:47:52

标签: javascript asynchronous express

这是发生了什么。我先保存新公司,然后在保存之前将_id附加到每个新用户。我遇到的问题是返回一个回复。当我把res.json()放入重复的函数中时,显然我得到一个错误,因为我已经有第一次循环时发送的响应。

那么,我如何调用signupSeq(record,res)但是等待异步方法完成所以我知道我是否有错误?

    var signupSeq = function(req, res) {

        async.waterfall([
            function(callback) {
                    console.log(req);
                    if (req.company._id===undefined){
                        var company = new Company(req.company);
                        company.save(function(err){
                            if (err) {
                                console.log('save error');
                                callback(err);
                            }else{
                                callback(null, company._id);
                            }
                        })
                    }else{
                        callback(null, req.company._id); //pass teh plain ID if it's not a new name:xxx
                    }

            },
            function(companyId, callback) {

                    delete req.company
                    req.company = companyId
                    // Init Variables
                    var user = new User(req);
                    var message = null;

                    // Add missing user fields
                    user.provider = 'local';
                    user.displayName = user.firstName + ' ' + user.lastName;

                        // Then save the user 
                    user.save(function(err) {
                        if (err) {
                            callback(err);

                        } else {
                            callback(null, user);
                        }
                    });
            }
        ], function (err, result) {
            if(err){
                console.log(result+'funciton result')
                return err
                // res.status(400).send({
                //  message: errorHandler.getErrorMessage(err)
                // });  
            }else{
                console.log(result+'funciton result')
                return result
                //res.json(result)
            }
        });
    }

    exports.saveMany = function(req, res){
            async.each(req.body, function(record, callback) {

              // Perform operation on record.body here.
              console.log('Processing record.body ' + record);

                // Do work to process record.body here
                var x = signupSeq(record, res)
                console.log(x+'<<<<<<<value of x');
                console.log('record.body processed');
                callback();

            }, function(err){
                // if any of the record.body processing produced an error, err would equal that error
                if( err ) {
                    res.json(err);
                  // One of the iterations produced an error.
                  // All processing will now stop.
                  console.log('A record.body failed to process');
                } else {
                    res.json('Success');
                  console.log('All files have been processed successfully');
                }
            });
    }

1 个答案:

答案 0 :(得分:1)

您可以在signupSeg功能中添加回调(cb)。

var signupSeq = function(req, res, cb) {

        async.waterfall([
            function(callback) {
                    console.log(req);
                    if (req.company._id===undefined){
                        var company = new Company(req.company);
                        company.save(function(err){
                            if (err) {
                                console.log('save error');
                                callback(err);
                            }else{
                                callback(null, company._id);
                            }
                        })
                    }else{
                        callback(null, req.company._id); //pass teh plain ID if it's not a new name:xxx
                    }

            },
            function(companyId, callback) {

                    delete req.company
                    req.company = companyId
                    // Init Variables
                    var user = new User(req);
                    var message = null;

                    // Add missing user fields
                    user.provider = 'local';
                    user.displayName = user.firstName + ' ' + user.lastName;

                        // Then save the user 
                    user.save(function(err) {
                        if (err) {
                            callback(err);

                        } else {
                            callback(null, user);
                        }
                    });
            }
        ], function (err, result) {
            if(err){
                console.log(result+'funciton result')
                cb(err)
                // res.status(400).send({
                //  message: errorHandler.getErrorMessage(err)
                // });  
            }else{
                console.log(result+'funciton result')
                cb(null,result)
                //res.json(result)
            }
        });
    }

exports.saveMany = function(req, res){
            async.each(req.body, function(record, callback) {

              // Perform operation on record.body here.
              console.log('Processing record.body ' + record);

                // Do work to process record.body here
                signupSeq(record, res,function(err,result){
                    var x= result;
                    console.log(x+'<<<<<<<value of x');
                    console.log('record.body processed');
                    callback();
                })


            }, function(err){
                // if any of the record.body processing produced an error, err would equal that error
                if( err ) {
                    res.json(err);
                  // One of the iterations produced an error.
                  // All processing will now stop.
                  console.log('A record.body failed to process');
                } else {
                    res.json('Success');
                  console.log('All files have been processed successfully');
                }
            });
    }

这样在asyn.each中,signipSeg必须在callback()调用之前完成。 希望这会有所帮助。