Cannot set headers after they are sent to the client

Wappler Version : 3.5.4
Operating System : Catalina
Server Model: NodeJS
Database Type: PG
Hosting Type: Local

Actual behavior

What actually happens?

How to reproduce

I’ve seen it happen in several parts of the app but the easiest way to reproduce is:

  1. Restrict a page and redirect elsewhere if not logged/authorized

Please check if it still happens with Wappler 3.5.5

I am afraid it is still happening.

Same error for the headers but now a previous error is appearing just after the restrict. I don’t know if they are both related or not. And to be honest I am not 100% sure the security restrict error was not there before. I think not. But…

  server-connect:app Executing action step restrict +43s
  server-connect:app options: {
  server-connect:app   provider: 'security1',
  server-connect:app   permissions: [ 'isVerified' ],
  server-connect:app   loginUrl: '/../login',
  server-connect:app   forbiddenUrl: '/../login'
  server-connect:app } +0ms
  server-connect:server Got error? error: select "id" from "user" where "id" = $1 and ("verified" = $2) - invalid input syntax for type integer: "false"
  server-connect:server     at Parser.parseErrorMessage (/Users/jon/Documents/dev/mpc-node/node_modules/pg-protocol/dist/parser.js:278:15)
  server-connect:server     at Parser.handlePacket (/Users/jon/Documents/dev/mpc-node/node_modules/pg-protocol/dist/parser.js:126:29)
  server-connect:server     at Parser.parse (/Users/jon/Documents/dev/mpc-node/node_modules/pg-protocol/dist/parser.js:39:38)
  server-connect:server     at Socket.<anonymous> (/Users/jon/Documents/dev/mpc-node/node_modules/pg-protocol/dist/index.js:8:42)
  server-connect:server     at Socket.emit (events.js:210:5)
  server-connect:server     at addChunk (_stream_readable.js:308:12)
  server-connect:server     at readableAddChunk (_stream_readable.js:289:11)
  server-connect:server     at Socket.Readable.push (_stream_readable.js:223:10)
  server-connect:server     at TCP.onStreamRead (internal/stream_base_commons.js:182:23) {
  server-connect:server   length: 106,
  server-connect:server   name: 'error',
  server-connect:server   severity: 'ERROR',
  server-connect:server   code: '22P02',
  server-connect:server   detail: undefined,
  server-connect:server   hint: undefined,
  server-connect:server   position: undefined,
  server-connect:server   internalPosition: undefined,
  server-connect:server   internalQuery: undefined,
  server-connect:server   where: undefined,
  server-connect:server   schema: undefined,
  server-connect:server   table: undefined,
  server-connect:server   column: undefined,
  server-connect:server   dataType: undefined,
  server-connect:server   constraint: undefined,
  server-connect:server   file: 'numutils.c',
  server-connect:server   line: '259',
  server-connect:server   routine: 'pg_strtoint32'
  server-connect:server } +43s
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:485:11)
    at ServerResponse.header (/Users/jon/Documents/dev/mpc-node/node_modules/express/lib/response.js:771:10)
    at ServerResponse.send (/Users/jon/Documents/dev/mpc-node/node_modules/express/lib/response.js:170:12)
    at ServerResponse.json (/Users/jon/Documents/dev/mpc-node/node_modules/express/lib/response.js:267:15)
    at /Users/jon/Documents/dev/mpc-node/lib/server.js:96:29
    at Layer.handle_error (/Users/jon/Documents/dev/mpc-node/node_modules/express/lib/router/layer.js:71:5)
    at trim_prefix (/Users/jon/Documents/dev/mpc-node/node_modules/express/lib/router/index.js:315:13)
    at /Users/jon/Documents/dev/mpc-node/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/jon/Documents/dev/mpc-node/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/jon/Documents/dev/mpc-node/node_modules/express/lib/router/index.js:275:10)
    at Layer.handle_error (/Users/jon/Documents/dev/mpc-node/node_modules/express/lib/router/layer.js:67:12)
    at trim_prefix (/Users/jon/Documents/dev/mpc-node/node_modules/express/lib/router/index.js:315:13)
    at /Users/jon/Documents/dev/mpc-node/node_modules/express/lib/router/index.js:284:7
    at Function.process_params (/Users/jon/Documents/dev/mpc-node/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/jon/Documents/dev/mpc-node/node_modules/express/lib/router/index.js:275:10)
    at next (/Users/jon/Documents/dev/mpc-node/node_modules/express/lib/router/route.js:127:14)
  server-connect:router Serving templateView /login +36ms


The security restrict is the only step in a SC file that is executed on layout route.

I can confirm it is related to the restrict permission check. If I remove the permission check and I leave only redirect if not logged the error disappears.

It seems that the permission is being checked for non-logged users which will end up in error and produce the header error.

Shouldn’t the permission be checked only if the user is logged?

I can confirm I am also seeing this behaviour. In my case, I am calling a SC on page load. If it returns 401, I redirect user to login route. Removing the server connect removes the error.
I wasn’t sure if it was Wappler or me, but from this post it looks like its Wappler.

1 Like

Please test following update, unzip in lib/auth. (1.4 KB)


Confirmed. It works.

Thanks @patrick

Is this only an issue with NodeJS?

Yes, this specific error in node only


That fixes it. Thanks.