-
-
Notifications
You must be signed in to change notification settings - Fork 16.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
double next
call leads to cryptic error when req.params
is called again
#2701
Comments
next
call leads to cryptic error when using with req.params
next
call leads to cryptic error when req.params
is called again
Are you sure that error has anything to do with double-calling |
Example:
|
Hm, let me double check that. Unfortunately I lost the original stack trace so was trying to remember from my sticky-notes how to reproduce. It was deff something around double callbacks and params. Let me dig more. |
@dougwilson ok figured out how to reproduce the issue I saw. It had to do with multiple next calls and params handling. See below. var express = require('./');
var supertest = require('supertest');
var app = express();
app.use(function(req, res, next) {
// double callback here leads to cryptic error (see below)
// I think there should be better protection against invoking twice, thoughts?
next();
next();
});
app.param('foo_id', function(req, res, next, id) {
// when this next is called after response is sent
// then there is a cryptic error
setTimeout(next, 1000);
});
app.get('/:foo_id', function(req, res) {
res.send('hello');
});
supertest(app)
.get('/foo')
.end(function(err, res) {
console.log(res.text);
});
/*
/Users/dz/projects/express/lib/router/index.js:392
paramCalled.value = req.params[key.name];
^
TypeError: Cannot read property 'foo_id' of undefined
at paramCallback [as _onTimeout] (/Users/dz/projects/express/lib/router/index.js:392:35)
at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
*/ Has to do with internal express params handling clearing the params. |
I still separately think that we could help the user better when calling |
Gotcha. There are people likely grand-fathered into doing this (looking through issues here, are are people who even posted code calling |
Do you want this fixed up in the router? I can create a PR for the behavior around protecting against double next. Do you think there is a valid/good/reasonable use case for supporting double next? My experience has been that it leads to more sadness than anything else but would love to hear what clever hacks are possible with double next that are not otherwise. |
Yes, we should fix this up for router 2.0/Express 5.0. I had, a long time ago, jotted down some thoughts here: senchalabs/connect#1042 |
An issue I see is that it would be nice to notify the user of a double next callback call, however we can't do that by passing the error to Alternative is to throw but that just seems like a bad time. I would be more inclined to make them event emitters and let the user handle routing errors that can't be tied to a particular request. Final alternative is to make the error and pass it to |
Yea, but I have always thought that it probably should be OK to "double call next", but only if you have an error to provide. The main reason is not only because it's the only mechanism to provide errors upstream using the current model, but also because you could have theoretically called "next()" all normal-like, but are still doing some kind of async processing related to said request and want to "next(err)" an error about it. |
Fair. One last alternative I thought about is to emit the error on request. Let me know which approach you favor and I will PR that against the new router module. |
I'm up for whatever, and of course, it's always easier to just accept a PR :) As for emitting an |
pillarjs/router#26 for reference. |
The following snippet shows the failure scenario. A user is calling
next()
twice in some middleware (due to whatever) and then as a result, thereq.params(...)
call in a route handler fails with a less than helpful error message. I can't think of a reason for double callingnext()
to be supported so maybe we can error out quicker or with something more helpful seems sane and safe for users.The text was updated successfully, but these errors were encountered: