Lucas Basquerotto
Lucas Basquerotto

Reputation: 8063

Error in NodeJS when using express session with Redis

I have a project in Node.js where I used MemoryStore from express-session, but when I changed to use the session with redis (connect-redis) I receive the error ERR wrong number of arguments for 'set' command when accessing any page and cannot log in (because the session is not created, or is created incorrectly).

Trying to find why this is happening I created a simple project to see what is causing the error, but even after a lot of search I could not find the reason. I followed the instructions in https://github.com/tj/connect-redis and saw a lot of examples here in https://stackoverflow.com/ but the error is still happening. I tested in more ways (commented lines and others), but it's still happening. It only occurs when acessing a page (localhost:3000, there's only this page in this test), not on the server start.


The package.json file:

{
  "name": "test",
  "description": "Test",
  "version": "0.0.1",
  "private": false,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "^4.13.1",
    "express-load": "^1.1.15",
    "express-session": "^1.13.0",
    "cookie-parser": "^1.4.1",
    "redis": "^2.4.2",
    "connect-redis": "^3.0.2",
    "ejs": "^2.4.1"
  }
}

The app.js file:

const SECRET = 'test';

var express = require('express')
  , load = require('express-load')
  , expressSession = require('express-session')
  , cookieParser = require('cookie-parser')
  , cookie = cookieParser(SECRET)
  //, cookie = cookieParser()
  , redis = require('redis')
  , RedisStore = require('connect-redis')(expressSession)
  //, redisClient = redis.createClient()
  , redisClient = redis.createClient({host: '127.0.0.1', port: '6379'})
  , store = new RedisStore({client: redisClient})
  //, store = new RedisStore({host: '127.0.0.1', port: '6379'})
  //, store = new expressSession.MemoryStore()
  , session = expressSession({
      store: store,
      secret: SECRET, 
      resave: true, 
      saveUninitialized: true
    })
  , app = express()
  , server = require('http').Server(app)
;

app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(cookie);
app.use(session);

load('controllers')
  .then('routes')
  .into(app)
;

server.listen(3000, function() {
  console.log('Testing...');
});

If I comment , store = new RedisStore({client: redisClient}) and uncomment , store = new expressSession.MemoryStore(), I don't receive the error and it works fine with sessions. This test project is very simple and I find it weird that the error still happens, when I do the same thing like many other examples that I saw.

This test project - aside from the node_modules - only has 1 page /views/home/index.ejs, 1 controller /controllers/home.js and 1 route /routes/home.js, but they are very simple and the only thing they do is lead the user to the simple page /views/home/index.ejs, so the error must not be in one of those.


Edit

The error stack trace:

[path]\test>npm start

> [email protected] start [path]\test
> node app.js

Testing...
Error: ERR wrong number of arguments for 'set' command
    at JavascriptReplyParser._parseResult ([path]\test\node_modules\redis\lib\parsers\javascript.js:43:16)
    at JavascriptReplyParser.try_parsing ([path]\test\node_modules\redis\lib\parsers\javascript.js:114:21)
    at JavascriptReplyParser.run ([path]\test\node_modules\redis\lib\parsers\javascript.js:126:22)
    at JavascriptReplyParser.execute ([path]\test\node_modules\redis\lib\parsers\javascript.js:107:10)
    at Socket.<anonymous> ([path]\test\node_modules\redis\index.js:131:27)
    at emitOne (events.js:77:13)
    at Socket.emit (events.js:169:7)
    at readableAddChunk (_stream_readable.js:146:16)
    at Socket.Readable.push (_stream_readable.js:110:10)
    at TCP.onread (net.js:523:20)

Upvotes: 2

Views: 2762

Answers (1)

Aaron Franco
Aaron Franco

Reputation: 1580

Update your version of Redis server.

https://github.com/redis/redis-rb/issues/372

Upvotes: 1

Related Questions