gpt4 book ai didi

javascript - 抛出新的类型错误 ('Invalid argument type' );来自 {mypath}\Desktop\reddit\node_modules\@redis\client\dist\lib\client\RESP2\encoder.js :17 t

转载 作者:行者123 更新时间:2023-12-03 08:03:51 30 4
gpt4 key购买 nike

我正在关注 ben 的 typescript GraphQL Redis 教程我是 typescript 新手,第一次尝试 redis当我添加属性 req.session.userId= user.id;

类型错误:参数类型无效在encodeCommand (C:\Users\vikash patel\Desktop\reddit\node_modules@redis\client\dist\lib\client\RESP2\encoder.js:17:19

我的 main.ts 代码

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"));

我的用户.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
};
}

我的类型.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;
}

我真的很笨...请帮忙:)

最佳答案

您需要创建启用 legacyMode 的 Redis 客户端:

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

如果您使用的是最新的 redis v4 ( https://github.com/tj/connect-redis ),则这是 connect-redis 软件包的限制。

关于javascript - 抛出新的类型错误 ('Invalid argument type' );来自 {mypath}\Desktop\reddit\node_modules\@redis\client\dist\lib\client\RESP2\encoder.js :17 t,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73090074/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com