Node save error "Can't set headers after they are sent"
<p>I've seen this problem several times on SO and elsewhere, but still couldn't get my code working. When trying to create a new document in node from a POST request I get the following error:</p>

<pre><code>_http_outgoing.js:346
    throw new Error('Can\'t set headers after they are sent.');
    ^

Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:346:11)
    at ServerResponse.header (/Users/joshuaswiss/CGA-Code/Transfers/mean-transfers/api/node_modules/express/lib/response.js:718:10)
    at ServerResponse.send (/Users/joshuaswiss/CGA-Code/Transfers/mean-transfers/api/node_modules/express/lib/response.js:163:12)
    at ServerResponse.json (/Users/joshuaswiss/CGA-Code/Transfers/mean-transfers/api/node_modules/express/lib/response.js:249:15)
    at /Users/joshuaswiss/CGA-Code/Transfers/mean-transfers/api/controllers/transfersController.js:23:12
    at /Users/joshuaswiss/CGA-Code/Transfers/mean-transfers/api/node_modules/mongoose/lib/document.js:1743:19
    at handleError (/Users/joshuaswiss/CGA-Code/Transfers/mean-transfers/api/node_modules/hooks-fixed/hooks.js:40:22)
    at _next (/Users/joshuaswiss/CGA-Code/Transfers/mean-transfers/api/node_modules/hooks-fixed/hooks.js:46:22)
    at fnWrapper (/Users/joshuaswiss/CGA-Code/Transfers/mean-transfers/api/node_modules/hooks-fixed/hooks.js:186:18)
    at /Users/joshuaswiss/CGA-Code/Transfers/mean-transfers/api/node_modules/mongoose/lib/schema.js:234:13
    at complete (/Users/joshuaswiss/CGA-Code/Transfers/mean-transfers/api/node_modules/mongoose/lib/document.js:1128:7)
    at /Users/joshuaswiss/CGA-Code/Transfers/mean-transfers/api/node_modules/mongoose/lib/document.js:1157:20
    at ObjectId.SchemaType.doValidate (/Users/joshuaswiss/CGA-Code/Transfers/mean-transfers/api/node_modules/mongoose/lib/schematype.js:654:22)
    at /Users/joshuaswiss/CGA-Code/Transfers/mean-transfers/api/node_modules/mongoose/lib/document.js:1153:9
    at doNTCallback0 (node.js:428:9)
    at process._tickCallback (node.js:357:13)
[nodemon] app crashed - waiting for file changes before starting...
</code></pre>

<p>My code is below. When I console.log <code>request.body</code> I get <code>[object Object]</code>, and when I log <code>JSON.stringify(request.body)</code> I get my object that I want. I've tried playing around with adding a return in <code>transfer.save</code>, as the error is in that, but couldn't get it working. Any help would be much appreciated!</p>

<p>app.js</p>

<pre><code>var express              = require('express');
var app                  = express();
var logger               = require('morgan');
var mongoose             = require('mongoose');
var cookieParser         = require('cookie-parser');
var bodyParser           = require('body-parser');
var multer               = require('multer');
var path                 = require('path');
var ejs                  = require('ejs');
var cors                 = require('cors');
var passport             = require('passport');
var port                 = process.env.PORT || 3000;

mongoose.connect('mongodb://localhost:27017/rsstransfers');

var routes = require('./config/routes');

app.use(cors());
app.use(logger('dev'));
app.use(bodyParser.json({limit: '50mb'}));
app.use(bodyParser.urlencoded({limit: '50mb', extended: true, parameterLimit: 5000}));
app.use(routes);

//runs server on port 3000
app.listen(3000, function() {
        console.log('server has been started on port %s', port);
});
</code></pre>

<p>transferController.js</p>

<pre><code>var Transfer = require('../models/Transfer');

//Get all transfers
function getAll(request, response) {
    Transfer.find(function(error, transfers) {
        if(error) response.json({message: 'couldn\'t find any transfers'});

        response.json({ transfers: transfers });
    }).select('-__v');
}

//Create a new transfer
function createTransfer(request, response) {
    console.log('posting');
    console.log('body: ' + request.body);
    console.log(JSON.stringify(request.body));

    var transfer = new Transfer(request.body);
    debugger;
    transfer.save(function(error) {
        if(error) response.json({ message: 'could not create transfer because ' + error });

        response.json({ transfer: transfer });
    });
}

//Delete
function removeTransfer(request, response) {
  var id = request.params.id;

  Transfer.remove({_id: id}, function(error) {
    if(error) response.json({message: 'Could not delete transfer b/c:' + error});

    response.json({message: 'Transfer successfully deleted'});
  }).select('-__v');
}

module.exports = {
    getAll: getAll,
    createTransfer: createTransfer,
    removeTransfer: removeTransfer
}
</code></pre>