最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - throw new TypeError('Invalid argument type'); from{mypath}Desktopredditnode_modules@redisclientdist

programmeradmin2浏览0评论

I am following ben's typescript GraphQL Redis tutorial I am new to typescript and first time trying redis when I add the property req.session.userId= user.id;

TypeError: Invalid argument type at encodeCommand (C:\Users\vikash patel\Desktop\reddit\node_modules@redis\client\dist\lib\client\RESP2\encoder.js:17:19

my main.ts code

declare module "express-session" {
interface SessionData {
userId: number | undefined;
}
}

import connectRedis from "connect-redis";
import { MyContext } from "./types";

const main = async () => {
const orm = await MikroORM.init(microConfig);
await orm.getMigrator().up();
const app = express(); 

const RedisStore = connectRedis(session);
const redisClient = redis.createClient() as any;
await redisClient.connect();
console.log("redis connected",redisClient.isOpen);

app.use(
  session({
    name: "qid",
    store: new RedisStore({
      client: redisClient,
      disableTouch: true,
      // disableTTL: true,
    }),
    cookie: {
      maxAge: 1000 * 60 * 60 * 24 * 365, //1year
      httpOnly: true,
      sameSite: "lax", //protecting csrf
      // secure:__prod__  //cookie only works in https
    },
    secret: "hellovikash",
    resave: false,
    saveUninitialized: false,
  })
);

const apolloserver = new ApolloServer({
  schema: await buildSchema({
    resolvers: [HelloResolver, PostResolver, UserResolver],
    validate: false,
}),
context: ({ req, res }): MyContext => ({ em: orm.em, req, res }),
});

await apolloserver.start();

apolloserver.applyMiddleware({ app });

// app.get("/",(_,res)=>{
//   res.send("ehllo")
// })

app.listen(4000, () => console.log("server is running on port 4000"));

my user.ts

@Mutation(() => USerResponse)
  async login(@Arg('options', () => UsernamePasswordInput) options: 
UsernamePasswordInput,
      @Ctx() { em, req }: MyContext
  ): Promise<USerResponse> {
    const user = await em.findOne(User, { username: options.username.toLowerCase() })
    if (!user) {
        return {
            errors: [{
                field: "username",
                message: "could not find username"
            }]
        }
    }
    const valid = await argon2.verify(user.password, options.password);
    if (!valid) {
        return {
            errors: [{
                field: "username",
                message: "password is incorrect"
            }]
        }
    }

      req.session.userId= user.id;   //This is the line which is creating error

      return {
        user
      };
  }

my types.ts

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

export type MyContext = {
  em: EntityManager<any> & EntityManager<IDatabaseDriver<Connection>>
  // req: Request;
  req: Request & { session: Express.SessionStore };
  res: Response;
}

I am really dumb...please help :)

I am following ben's typescript GraphQL Redis tutorial I am new to typescript and first time trying redis when I add the property req.session.userId= user.id;

TypeError: Invalid argument type at encodeCommand (C:\Users\vikash patel\Desktop\reddit\node_modules@redis\client\dist\lib\client\RESP2\encoder.js:17:19

my main.ts code

declare module "express-session" {
interface SessionData {
userId: number | undefined;
}
}

import connectRedis from "connect-redis";
import { MyContext } from "./types";

const main = async () => {
const orm = await MikroORM.init(microConfig);
await orm.getMigrator().up();
const app = express(); 

const RedisStore = connectRedis(session);
const redisClient = redis.createClient() as any;
await redisClient.connect();
console.log("redis connected",redisClient.isOpen);

app.use(
  session({
    name: "qid",
    store: new RedisStore({
      client: redisClient,
      disableTouch: true,
      // disableTTL: true,
    }),
    cookie: {
      maxAge: 1000 * 60 * 60 * 24 * 365, //1year
      httpOnly: true,
      sameSite: "lax", //protecting csrf
      // secure:__prod__  //cookie only works in https
    },
    secret: "hellovikash",
    resave: false,
    saveUninitialized: false,
  })
);

const apolloserver = new ApolloServer({
  schema: await buildSchema({
    resolvers: [HelloResolver, PostResolver, UserResolver],
    validate: false,
}),
context: ({ req, res }): MyContext => ({ em: orm.em, req, res }),
});

await apolloserver.start();

apolloserver.applyMiddleware({ app });

// app.get("/",(_,res)=>{
//   res.send("ehllo")
// })

app.listen(4000, () => console.log("server is running on port 4000"));

my user.ts

@Mutation(() => USerResponse)
  async login(@Arg('options', () => UsernamePasswordInput) options: 
UsernamePasswordInput,
      @Ctx() { em, req }: MyContext
  ): Promise<USerResponse> {
    const user = await em.findOne(User, { username: options.username.toLowerCase() })
    if (!user) {
        return {
            errors: [{
                field: "username",
                message: "could not find username"
            }]
        }
    }
    const valid = await argon2.verify(user.password, options.password);
    if (!valid) {
        return {
            errors: [{
                field: "username",
                message: "password is incorrect"
            }]
        }
    }

      req.session.userId= user.id;   //This is the line which is creating error

      return {
        user
      };
  }

my types.ts

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

export type MyContext = {
  em: EntityManager<any> & EntityManager<IDatabaseDriver<Connection>>
  // req: Request;
  req: Request & { session: Express.SessionStore };
  res: Response;
}

I am really dumb...please help :)

Share Improve this question asked Jul 23, 2022 at 10:48 VikashVikash 1231 silver badge11 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You need to create the Redis client with legacyMode enabled:

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

It's a limitation of connect-redis package if you are using the newest redis v4 (https://github./tj/connect-redis).

If you're ing here in 2023 using [email protected], it can be tiring especially if you require backward patibility.

In my case, I was previously using [email protected]. Here's a quick solution.

import { createClient } from "redis";
const redis_url: string = REDIS_URL;
const a = URL.parse(redis_url);

const redisClient: any = createClient({
 url: a.href,
 legacyMode: true // Add this to createClient
});

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论