dchester / epilogue

Create flexible REST endpoints and controllers from Sequelize models in your Express app
846 stars 116 forks source link

Cannot set headers after they are sent to the client #239

Closed jonwrona closed 5 years ago

jonwrona commented 5 years ago

Trying to get a very simple setup of epilogue working with an sqlite database. Apparently headers are being set after they are sent to the client. This happens on any attempt to use one of the epilogue routes.

Unhandled rejection Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

For a create call the error points to res.header('Location', location);, which is on line 46 of the create.js Controller.

I am using Node v12.4.0.

Here is the entire error:

Executing (default): SELECT count(*) AS `count` FROM `whitelists` AS `whitelists`;
Executing (default): SELECT `id`, `blueGroupName`, `createdAt`, `updatedAt` FROM `whitelists` AS `whitelists` LIMIT 0, 100;
Unhandled rejection Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:455:11)
    at ServerResponse.header (/Users/jonwrona/Documents/GitHub/whitelister/api/node_modules/express/lib/response.js:771:10)
    at /Users/jonwrona/Documents/GitHub/whitelister/api/node_modules/epilogue/lib/Controllers/list.js:170:13
    at tryCatcher (/Users/jonwrona/Documents/GitHub/whitelister/api/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/jonwrona/Documents/GitHub/whitelister/api/node_modules/bluebird/js/release/promise.js:517:31)
    at Promise._settlePromise (/Users/jonwrona/Documents/GitHub/whitelister/api/node_modules/bluebird/js/release/promise.js:574:18)
    at Promise._settlePromise0 (/Users/jonwrona/Documents/GitHub/whitelister/api/node_modules/bluebird/js/release/promise.js:619:10)
    at Promise._settlePromises (/Users/jonwrona/Documents/GitHub/whitelister/api/node_modules/bluebird/js/release/promise.js:699:18)
    at Promise._fulfill (/Users/jonwrona/Documents/GitHub/whitelister/api/node_modules/bluebird/js/release/promise.js:643:18)
    at Promise._resolveCallback (/Users/jonwrona/Documents/GitHub/whitelister/api/node_modules/bluebird/js/release/promise.js:437:57)
    at Promise._settlePromiseFromHandler (/Users/jonwrona/Documents/GitHub/whitelister/api/node_modules/bluebird/js/release/promise.js:529:17)
    at Promise._settlePromise (/Users/jonwrona/Documents/GitHub/whitelister/api/node_modules/bluebird/js/release/promise.js:574:18)
    at Promise._settlePromise0 (/Users/jonwrona/Documents/GitHub/whitelister/api/node_modules/bluebird/js/release/promise.js:619:10)
    at Promise._settlePromises (/Users/jonwrona/Documents/GitHub/whitelister/api/node_modules/bluebird/js/release/promise.js:699:18)
    at Promise._fulfill (/Users/jonwrona/Documents/GitHub/whitelister/api/node_modules/bluebird/js/release/promise.js:643:18)
    at PromiseArray._resolve (/Users/jonwrona/Documents/GitHub/whitelister/api/node_modules/bluebird/js/release/promise_array.js:126:19)
    at PromiseArray._promiseFulfilled (/Users/jonwrona/Documents/GitHub/whitelister/api/node_modules/bluebird/js/release/promise_array.js:144:14)
    at Promise._settlePromise (/Users/jonwrona/Documents/GitHub/whitelister/api/node_modules/bluebird/js/release/promise.js:579:26)
    at Promise._settlePromise0 (/Users/jonwrona/Documents/GitHub/whitelister/api/node_modules/bluebird/js/release/promise.js:619:10)
    at Promise._settlePromises (/Users/jonwrona/Documents/GitHub/whitelister/api/node_modules/bluebird/js/release/promise.js:699:18)
    at _drainQueueStep (/Users/jonwrona/Documents/GitHub/whitelister/api/node_modules/bluebird/js/release/async.js:138:12)
    at _drainQueue (/Users/jonwrona/Documents/GitHub/whitelister/api/node_modules/bluebird/js/release/async.js:131:9)

I can post my code as well if necessary, the server is very small.

jonwrona commented 5 years ago

index.js

const express = require('express');
const bodyParser = require('body-parser');
const { promisify } = require('util');
const initializeDatabase = require('./database');

const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

const startServer = async () => {
  await initializeDatabase(app);
  const port = process.env.PORT || 3000;
  await promisify(app.listen).bind(app)(port);
  console.log(`Listening on port ${port}`);
};

startServer();

database.js

const Sequelize = require('sequelize');
const epilogue = require('epilogue');

const database = new Sequelize({
  dialect: 'sqlite',
  storage: './test.sqlite',
});

const Whitelist = database.define('whitelists', {
  blueGroupName: Sequelize.STRING,
});

const initializeDatabase = async (app) => {
  epilogue.initialize({ app, sequelize: database });

  epilogue.resource({
    model: Whitelist,
    // endpoints: [ `/whitelists`, `/whitelists/:id` ],
  });

  await database.sync();
};

module.exports = initializeDatabase;
jonwrona commented 5 years ago

Here is the package.json if dependency versions could be the issue:

{
  "name": "whitelister-api",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "dependencies": {
    "body-parser": "^1.19.0",
    "epilogue": "^0.7.1",
    "express": "^4.17.1",
    "sequelize": "^5.9.4",
    "sqlite": "^3.0.3",
    "sqlite3": "^4.0.9",
    "util": "^0.12.1"
  }
}
shawnbissell commented 5 years ago

I just started seeing this today as well when we upgraded to using sequelize v5

tymoteuszKlocek commented 5 years ago

I had the same issue, try to use this: https://github.com/tommybananas/finale

jonwrona commented 5 years ago

@tymoteuszKlocek I grabbed that and it solved my issues! Thanks.