Latest master (newer than 2.2.16) throws exception
Issue Description
The latest master or newer than 2.2.16 is throwing an exception and crashing whenever making a query.
Whenever a query is made the following error occurs in the logs:
Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11) at expressInit (.../node_modules/express/lib/middleware/init.js:23:42) at Layer.handle as handle_request at trim_prefix (.../node_modules/express/lib/router/index.js:312:13) at .../node_modules/express/lib/router/index.js:280:7 at Function.process_params (.../node_modules/express/lib/router/index.js:330:12) at next (.../node_modules/express/lib/router/index.js:271:10) at query (.../node_modules/express/lib/middleware/query.js:49:5) at Layer.handle as handle_request at trim_prefix (.../node_modules/express/lib/router/index.js:312:13)
I couldn't find anything on ParseServer.js:431 that seemed related though. Using [email protected] works just fine.
Steps to reproduce
Please include a detailed list of steps that reproduce the issue. Include curl commands when applicable.
- Install the latest parse-server (npm install parse-server)
- Launch the server. (I'm using forever to run it with node)
- Make any cloud request with a query.
Expected Results
No error.
Actual Outcome
Server throws exception: Error: Can't set headers after they are sent.
Environment Setup
- Server
- parse-server version: 2.2.17 ?
- Operating System: Ubuntu
- Localhost or remote server? (AWS, Heroku, Azure, Digital Ocean, etc): Azure
- Database
- MongoDB version: N/A
- Storage engine: N/A
- Hardware: N/A
- Localhost or remote server? (AWS, mLab, ObjectRocket, Digital Ocean, etc): Azure
Logs/Trace
Oddly enough VERBOSE made the error message go away but the problem still exists.
However, from my own logs I can tell this is happening every time a query is made.
Yes we have same error, we have rolled back as it was crashing our app
https://github.com/ParsePlatform/parse-server/pull/2338
Same here...
I had the same issue after I updated as well.
I am using express to catch any errors, so what I did was check if the response headers had already been sent to the client before handling my errors.
This seems to take care of it for me for now so I can continue with the latest parse, but not sure if there is a fix or better solution to this out there.
// catch 404 and forward to error handler
app.use(function(req, res, next) {
// Fixes issue with cloud code
if (res.headersSent) {
return next(err);
}
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
// Fixes issue with cloud code
if (!res.headersSent) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
}
});
There is some documentation for this also on the express site at the bottom under the heading "The Default Error Handler".
http://expressjs.com/en/guide/error-handling.html
Closing as duplicate of #2362