I use express-session with a redis store in my NodeJS backend. When I just used javascript, everything worked well:
const express = require('express');
const session = require('express-session');
const redis = require('redis');
const RedisStore = require('connect-redis')(session);
const app = express();
let redisClient = redis.createClient(6380, process.env.REDISCACHEHOSTNAME, {auth_pass: process.env.REDISCACHEKEY, tls: {servername: process.env.REDISCACHEHOSTNAME}});
app.use(session({
store: new RedisStore({ client: redisClient }),
}));
After upgrading to typescript, I wasn't able to reuse the same code. That's how my code looks now:
import express = require('express');
import session = require('express-session');
import redis = require('redis');
import connectRedis from 'connect-redis';
const app = express();
const RedisStore = connectRedis(session);
const redisClient = redis.createClient({
url: "rediss://" process.env.REDISCACHEHOSTNAME ":6380",
password: process.env.REDISCACHEKEY,
});
redisClient.connect().then(() => {
const redisStore = new RedisStore({ client: redisClient });
app.use(session({ store: redisStore }));
... some app routes
});
It seems to connect, but when I try to update my session like:
req.session.user = { ... }
I get the following error:
xxx\backend\node_modules@redis\client\dist\lib\client\RESP2\encoder.js:17 throw new TypeError('Invalid argument type'); ^ TypeError: Invalid argument type at encodeCommand (xxx\backend\node_modules@redis\client\dist\lib\client\RESP2\encoder.js:17:19)
at RedisCommandsQueue.getCommandToSend (xxx\backend\node_modules@redis\client\dist\lib\client\commands-queue.js:187:45)
What I'm doing wrong?
CodePudding user response:
I can reproduce the issue if I don't pass the legacyMode : true
option.
Strange thing is that the documentation states this option is required for Redis v4 and up, but I was able to reproduce the same issue on v3.2.5 when the option isn't set.