Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERR_INVALID_ARG_TYPE when setting req.session.userId #49

Open
osilkin98 opened this issue Dec 29, 2021 · 2 comments
Open

ERR_INVALID_ARG_TYPE when setting req.session.userId #49

osilkin98 opened this issue Dec 29, 2021 · 2 comments

Comments

@osilkin98
Copy link

osilkin98 commented Dec 29, 2021

Running into this issue where the server will crash when setting req.session.userId = user.id:

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

I've already set my types.ts file to include the workaround for the versions of express-session ^1.17.4:

import { EntityManager, IDatabaseDriver, Connection } from "@mikro-orm/core";
import { Request, Response } from "express";
import { Session, SessionData } from "express-session";

export type MyContext = {
  em: EntityManager<any> & EntityManager<IDatabaseDriver<Connection>>;
  res: Response;
  req: Request & {
    session: Session & Partial<SessionData> & { userId?: number };
  };
  // redis: Redis;
};

How would one go about setting the data in the request session data?

Full stack trace:

internal/process/promises.js:213
        triggerUncaughtException(err, true /* fromPromise */);
        ^

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
    at new NodeError (internal/errors.js:322:7)
    at Socket.Writable.write (internal/streams/writable.js:285:13)
    at RedisSocket.writeCommand (/Users/oleg/Programming/tutorials/lireddit-follow/server/node_modules/@node-redis/client/dist/lib/client/socket.js:57:130)
    at Commander._RedisClient_tick (/Users/oleg/Programming/tutorials/lireddit-follow/server/node_modules/@node-redis/client/dist/lib/client/index.js:421:64)
    at Commander._RedisClient_sendCommand (/Users/oleg/Programming/tutorials/lireddit-follow/server/node_modules/@node-redis/client/dist/lib/client/index.js:402:82)
    at Commander.commandsExecutor (/Users/oleg/Programming/tutorials/lireddit-follow/server/node_modules/@node-redis/client/dist/lib/client/index.js:160:154)
    at Commander.BaseClass.<computed> [as set] (/Users/oleg/Programming/tutorials/lireddit-follow/server/node_modules/@node-redis/client/dist/lib/commander.js:8:29)
    at RedisStore.set (/Users/oleg/Programming/tutorials/lireddit-follow/server/node_modules/connect-redis/lib/connect-redis.js:65:21)
    at Session.save (/Users/oleg/Programming/tutorials/lireddit-follow/server/node_modules/express-session/session/session.js:72:25)
    at Session.save (/Users/oleg/Programming/tutorials/lireddit-follow/server/node_modules/express-session/index.js:406:15)
    at ServerResponse.end (/Users/oleg/Programming/tutorials/lireddit-follow/server/node_modules/express-session/index.js:335:21)
    at ServerResponse.send (/Users/oleg/Programming/tutorials/lireddit-follow/server/node_modules/express/lib/response.js:221:10)
    at /Users/oleg/Programming/tutorials/lireddit-follow/server/node_modules/apollo-server-express/dist/ApolloServer.js:89:25 {
  code: 'ERR_INVALID_ARG_TYPE'
}
@osilkin98
Copy link
Author

Never mind; I managed to solve it.
For anyone looking for the solution, you have to set the redis client to use legacyMode in index.ts:

  const redisClient = redis.createClient({
    legacyMode: true,
  });

@osilkin98 osilkin98 reopened this Dec 29, 2021
@allez-allez
Copy link

allez-allez commented Jan 7, 2022

just ran into this issue and my solution was to switch to ioredis from redis, based on my reading there seems to be some sort of collision of types in redis v4.

additionally, ioredis uses new redis instead of redis.redisClient.

so the code that works for me is :

import redis from "ioredis"; 

const RedisStore = connectRedis(session);
const redisClient = new redis();

and the types.ts export type is unchanged

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants