【问题标题】:TypeError [ERR_INVALID_ARG_TYPE], express-session+redis ErrorTypeError [ERR_INVALID_ARG_TYPE], express-session+redis 错误
【发布时间】:2022-01-10 03:42:38
【问题描述】:

我正在使用 typescript + express 进行开发

我想使用 Redis 到 Session Storage 并且我安装了 redis,connect-redis 我在下面写代码

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,
        }),
    })
);

我在下面写了代码来测试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');
    });
});

但是,将值保存到会话时出现以下错误。 TypeError [ERR_INVALID_ARG_TYPE]:“块”参数必须是字符串类型或 Buffer 或 Uint8Array 的实例。收到一个数组实例

因为这个错误,之前使用内存存储实现的本地护照代码不起作用。 请帮帮我..

【问题讨论】:

  • connect-redis版本为6
  • 当我在 node_modules/@node-redis/client/dist/lib/client/socket.js 第 61 行检查 console.log 时,确认 connect-redis 传递的参数看起来像数组。 Example [ 'sess: ~~~~' , '{"cookie":{cookieSetting...}, "key":"value"}', 'EX', ttl(number)] 不知道是哪个动作添加以使其正常工作。
  • github.com/tj/connect-redis/issues/336 16 天前有一个问题.. 当这些错误出现时,请检查所有这些错误。

标签: typescript express redis express-session


【解决方案1】:

使用当前基于时间的redis 4.0版本时出现问题,需要在redis设置中设置legacyMode: true。

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

【讨论】:

    猜你喜欢
    • 2020-12-03
    • 2017-10-28
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    • 2020-12-26
    相关资源
    最近更新 更多