Express define next middlewares on req condition

Issue

Let’s say i have something like:

function odd1(req, res, next) {
    console.log('odd1');
    return next();
}
function odd2(req, res, next) {
    console.log('odd2');
    return next();
}
function even1(req, res, next) {
    console.log('even1');
    return next();
}
function even2(req, res, next) {
    console.log('even2');
    return next();
}

app.get('/something', (req, res, next) => {
    if (req.query.type === 'odd') {
        // return odd1, odd2, next
    }
    // return even1, even2, next
}, (req, res) => {
    console.log('OK');
    return res.send('OK');
});

I want to define the next middlewares on a queryparam condition; if the type is ‘odd’ then continue to odd1, odd2, then send ‘OK’; otherwise it goes to even1, even2, then send ‘OK’

I’ve tried something like:

app.get('/something', (req, res, next) => {
    if (req.body.type === 'odd') {
        return odd1(req, res, odd2(req, res, next));
    }
    return even1(req, res, even2(req, res, next));
}, (req, res) => {
    console.log('OK');
    return res.send('OK');
});

But it doesn’t work properly

odd2
OK
odd1
TypeError: next is not a function

Is this idea possible to do?

Solution

Here is one line solution :-

Just Replace the third argument when calling odd1 function with the function that return odd2 for your next argument.

Inside the if statement, when type is odd :-

...
if ( req.query.type == 'odd') {
  return odd1(req, res, () => odd2(req, res, next));
}
...

In this way, you will get the right order i.e.,

odd1
odd2
OK

The main bug is that you make mistake in 3rd argument when calling odd1 beacuse it should be a function, but you passed a expression i.e., odd2() which return next(), which is also a expression because it has a () sign, which returns the third argument of app.get() and it returns null.

It means you returned null as the third argument for odd1 inside the if block when type is odd

Answered By – Abhay

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published