【问题标题】:async.each cannot set headers after already setasync.each 在已设置后无法设置标头
【发布时间】:2015-06-08 18:47:51
【问题描述】:

这就是发生的事情。我先保存新公司,然后在保存之前将 _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');
                }
            });
    }

【问题讨论】:

    标签: javascript asynchronous express


    【解决方案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() 之前完成。 希望这会有所帮助。

    【讨论】:

    • 此外,将async.each更改为async.map;将callback(); 更改为callback(null, result);;并将最终的function(err) 更改为function(err, results)。变量results 将包含调用signupSeq 的结果数组。在该函数中处理该数组,然后通过res 发送结果。
    猜你喜欢
    • 2013-04-06
    • 1970-01-01
    • 1970-01-01
    • 2014-08-23
    • 1970-01-01
    • 2015-12-20
    • 2011-10-17
    相关资源
    最近更新 更多