Search code examples
typescriptexpressredisexpress-session

TypeError [ERR_INVALID_ARG_TYPE], express-session+redis Error


I am developing with typescript + express

i want use Redis to Session Storage and i installed redis, connect-redis and i code below

import { createClient } from 'redis';
import * as RedisStore from 'connect-redis';
...
const client = createClient({
    url: 'redis://default:qwer1234@localhost:6379',
});
client.connect().then(() => {
    console.log('redis success');
});
...
app.use(
    session({
        secret: env.COOKIE_SECRET!,
        resave: false,
        saveUninitialized: false,
        store: new redisSession({
            client: client,
        }),
    })
);

and i wrote code below to test redis

app.get('/', (req, res) => {
    const sess = req.session;
    if (sess.key) {
        res.send(sess.key);
    } else {
        res.send('FAIL');
    }
});
app.post('/login', (req, res) => {
    const sess = req.session;
    const { username } = req.body;
    sess.key = username
    // add username and password validation logic here if you want.If user is authenticated send the response as success
    res.end('success');
});
app.get('/logout', (req, res) => {
    req.session.destroy((err) => {
        if (err) {
            return console.log(err);
        }
        res.send('OK');
    });
});

But , I get the following error when saving a value to the session. TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received an instance of Array

because this error, the passport-local code that was previously implemented using memory storage does not work. help me plz..


Solution

  • There is an issue when using the current time-based redis 4.0 version, so you need to set legacyMode: true in the redis setting.

    const client = createClient({
        url: yourURL,
        legacyMode: true,
    });