hidjou / classsed-react-firebase-functions

327 stars 180 forks source link

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

Open felipeavl opened 4 years ago

felipeavl commented 4 years ago

I have a problem with some of the functions , specifically the ones that query the database for some validation and then updates the documents.

The exports.commentOnScream for example. If I run it with an inexistent screamId I get the 404 response, but the code continues to the function on the next 'then()' and tries to return a json with the comment ( If I console.log something before res.json(newComment); it get printed ).

So I get the following error:

(node:27910) UnhandledPromiseRejectionWarning: 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 (/home//Desenvolvimento/socialape-functions/functions/node_modules/express/lib/response.js:771:10)
 at ServerResponse.send (/home//Desenvolvimento/socialape-functions/functions/node_modules/express/lib/response.js:170:12)
 at ServerResponse.json (/home//Desenvolvimento/socialape-functions/functions/node_modules/express/lib/response.js:267:15)
 at /home//Desenvolvimento/socialape-functions/functions/handlers/screams.js:95:23
 at processTicksAndRejections (internal/process/task_queues.js:94:5)

My code is exactly the same as the sreams.js in the repository. Any ideas of how to solve this ??

thilanka commented 4 years ago

Post the entire console log.

bmwwoo commented 4 years ago

I just wrote a Stack Overflow question about this too: https://stackoverflow.com/questions/63084656/proper-express-error-handling-with-promises-and-avoiding-err-http-headers-sent.

I'll update that if I find any solution.