Express not finding redis results it expects

Express not finding redis results it expects


I followed an express demo that has you storing values with redis and then retrieving them. When I tried to adapt that code to the express app I have set up I fail to get the expected results.

Mainly, when I try to get the length of I get an error as it has a null value.

With the below code I expect to have some value, instead it is always null and thus throws an error.

The code in this paste DOES work ( will equal, say, 2):

The code in this paste NOT work ( is null, throws error trying to check length) :

When I check via the redis cli using:

ZREVRANGEBYSCORE online +inf -inf

I can see the various user-agent values stored as expected.

What do I need to do to get this value back from redis and use it?

Problem courtesy of: Stuart


The problem is related to the order of the middleware settings. The order of the app.use calls is relevant. See the following question for more information:

Node.js / Express.js – How does app.router work?

When you have a doubt about the order of execution, it is useful to add some console.log traces in your code at various places to understand the flow.

Here, you call app.use(app.router) before setting your own middleware functions to get the data from Redis. So node.js routes the request before having a chance to call your middleware functions. The consequence is zrevrangebyscore is never called, and req.inline not set, when the get query is processed.

If you define and app.use the middleware functions before calling app.use(app.router), it will work.

Solution courtesy of: Didier Spezia


Leave a Reply

Your email address will not be published. Required fields are marked *