title: Unhandled rejection Error: Can't set headers after they are sent using Node
body: <p>I am attempting to render a view using JS, Node, and Express but am getting this error 'Unhandled rejection Error: Can't set headers after they are sent'. I know it's something to do with asynchronous callbacks, but 
  I can't work out exactly how to fix it.</p>
  
  <p>The problem doesn't seem to be with the res.redirect (removing it doesn't display the data) but rather with the <code>res.render</code> and <code>var spaces</code> not loading in properly to the EJS, we think.  When we put <code>spaces</code>, we get the message "object Promise".</p>
  
  <pre><code>const userController = require('../controllers').user;
  const spaceController = require('../controllers').space;
  
  module.exports = (app) =&gt; {
    app.get('/', function(req, res) {
        res.render('index.ejs');
    });
  
    app.post('/users/new', function(req,res){
      userController.create(req,res);
      res.redirect('/');
    });
  
    app.get('/spaces', function(req, res) {
      var spaces = spaceController.retrieve(req,res);
      res.render('spaces/index.ejs', {spaces});
  
    });
  
    app.get('/spaces/new', function(req, res) {
      res.render('spaces/new.ejs');
    });
  
    app.post('/spaces/new', function(req,res){
      spaceController.create(req,res);
      res.redirect('/spaces');
    });
  
  };
  </code></pre>
  
  <p>And my controller:</p>
  
  <pre><code>const Space = require('../models').Space;
  
  module.exports = {
    create(req, res) {
      return Space
      .create({
        name: req.body.name,
        description: req.body.description,
        price: req.body.price,
      })
      .then(space =&gt; res.status(201).send(space))
      .catch(error =&gt; res.status(400).send(error));
    },
  
    retrieve(req, res) {
      return Space
      .findAll()
      .then(space =&gt; {
        if (!space) {
          return res.status(404).send({
            message: 'Space Not Found',
          });
        }
        return res.status(200).send(space);
      })
      .catch(error =&gt; res.status(400).send(error));
    },
  };
  </code></pre>
  
  <p>And my view:</p>
  
  <pre><code>&lt;!doctype html&gt;
   &lt;html&gt;
   &lt;head&gt;
       &lt;title&gt;MakersBnB&lt;/title&gt;
       &lt;link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css"&gt; &lt;!-- load bootstrap css --&gt;
       &lt;link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css"&gt; &lt;!-- load fontawesome --&gt;
       &lt;style&gt;
           body        { padding-top:80px; }
       &lt;/style&gt;
   &lt;/head&gt;
   &lt;body&gt;
   &lt;div class="container"&gt;
  
   &lt;div class="col-sm-6 col-sm-offset-3"&gt;
     &lt;div class="jumbotron text-center"&gt;
  
       &lt;h1&gt;Book a Space&lt;/h1&gt;
  
  &lt;form action="/spaces/new" method="get"&gt;
    &lt;button type="submit" class="btn btn-warning btn-lg"&gt;List a space&lt;/button&gt;
  
  &lt;/form&gt;
  
  &lt;div class="spaces"&gt;
    &lt;% spaces.each(function(space) { %&gt;
          &lt;ul&gt;
            &lt;li&gt;&lt;%= space.name %&gt;&lt;/a&gt;&lt;/li&gt;
            &lt;li&gt;&lt;%= space.description %&gt;&lt;/li&gt;
            &lt;li&gt;£ &lt;%= space.price %&gt;&lt;/li&gt;
          &lt;/ul&gt;
          &lt;% });%&gt;
  &lt;/div&gt;
  
  
   &lt;/div&gt;
  
   &lt;/div&gt;
   &lt;/body&gt;
   &lt;/html&gt;
  </code></pre>