title: Node.js : res.redirect("back") got Error('Can\'t set headers after they are sent.');
body: <p>I have a little problem on an express.js app.
  Can't redirect back after several actions with callbacks.</p>
  
  <p>My applications works with objects called "markers" and they are rated by "sub categories"
  The goal of these actions is to merge 2 or more subcategories, move all markers from old to the new subcategoy, and finally, delete the old sub category.</p>
  
  <p>Here is the code :</p>
  
  <p>The action called after checked 2 or more subcategories : </p>
  
  <pre><code>exports.postMergeSubCategories = function(req, res) {
      "use strict";
  
      var data = {};
      data.subCategories  = req.body.subCategoriesChecked;
      data.enName         = req.body.subCategory.enName;
      data.frName         = req.body.subCategory.frName;
      data.deName         = req.body.subCategory.deName;
  
      if (data.subCategories.length &gt; 1) {
          sscategoryMapper.merge(data, function(err, sscategory) {
              if (err) return console.log(err);
              console.log ('Sub categories merged !');
              req.flash('mergeMessage', 'Merge completed!');
              res.redirect("back");
          });
      } else { // error
          console.log('Error while merge sub categories. It seems the number of sub categories checked less 2');
          req.flash('mergeMessage', 'An error occured whil merge');
          res.redirect("back");
      }    
  };
  </code></pre>
  
  <p>The function sscategoryMapper.merge :</p>
  
  <pre><code>module.exports.merge = function(data, callback) {
  async.waterfall([
      function(callback){ // create new sub category
          save(data, function(err, sscategory) {
              if (err) return callback(err);
              callback(null, sscategory._id);
          });
      },
      function(sscategoryId, callback){ // update marker2sscategory
          async.each(data.subCategories.split('|'), function(oldSscategoryId, err) { // for each subcategories, update markers2sscategory and remove the old subcategory
              async.waterfall([
                  function(callback) { // update maker2sscategory to set the new sscategoryId to all target markers
                      marker2sscategoryMapper.update(sscategoryId, oldSscategoryId, function(err) {
                          if (err) return callback(err);
                          callback(null, oldSscategoryId);
                      });
                  },
                  function(oldSscategoryId, callback) { // delete the old sscategory
                      remove(oldSscategoryId, function(err) {
                          if (err) return callback(err);
                          callback();
                      });
                  }
              ], function(err) {
                  if (err) return callback(err);
                  callback();
              });
          }, function(err) {
              callback(); 
          });
          callback(null, sscategoryId);
      }
  ], function (err, result) {
      if (err) return callback(err);
      callback(null, result);
  });
  };
  </code></pre>
  
  <p><strong>UPDATE</strong>
  First problem here : I called 2 times the callback ... This piece of code moved to that : </p>
  
  <pre><code>        }, function(err) {
              callback(null, sscategoryId);
          });
      }
  ], function (err, result) {
      if (err) return callback(err);
      callback(null, result);
  });
  };
  </code></pre>
  
  <p><strong>UPDATE END</strong></p>
  
  <p>The function marker2sscategoryMapper.update :</p>
  
  <pre><code>module.exports.update = function(to, from, callback) {
  // update marker2sscategory with the new subcategory
  dbMarker2sscategory.update({'_sscategory' : from}, {
      '_sscategory' : to
  }, {multi: true}, function(err) {
      if (err) return callback(new Error(err));
      callback(null, to);
  });
  }
  </code></pre>
  
  <p>And the function remove : </p>
  
  <pre><code>var remove = module.exports.remove = function(id, callback) {
  dbSscategory.remove({'_id' : id}, function(err) {
      if (err) return callback(new Error(err));
      callback(null, id);
  });
  };
  </code></pre>
  
  <p>If I comment "res.redirect("back")", it will works.
  Else, the error is : "Error('Can\'t set headers after they are sent.');" 2 times.</p>
  
  <p>I read that one of the causes can be duplicate callbacks ... But I don't see that in my code.</p>
  
  <p>Appreciate your help.</p>