我正在尝试在我的 NestJS 后端中实现
express-session
包以及 connect-redis
来进行会话存储,但是我收到了如下错误。我什至将商店内的客户端更改为RedisClient
,这是在提供程序文件中声明的注入令牌,但仍然没有运气,我到底错在哪里?实现此类功能的最佳实践是什么,以便我可以使用 redis 作为会话存储,无论是使用connect-redis
还是其他东西......提前非常感谢,任何帮助表示赞赏
错误信息:
store: new ((0, connect_redis_1.default)(session))({
^
TypeError: Class constructor RedisStore cannot be invoked without 'new'
ERROR in ./src/app.module.ts:34:5
TS2322: Type 'RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>' is not assignable to type 'Type<any> | DynamicModule | Promise<DynamicModule> | ForwardReference<any>'.
32 | CartModule,
33 | RedisModule,
> 34 | session({
| ^^^^^^^^^
> 35 | store: new (RedisStore(session))({
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 36 | client: redisClientFactory,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 37 | }),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 38 | secret: 'my-secret',
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 39 | resave: false,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 40 | saveUninitialized: false,
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 41 | }),
| ^^^^^^^
42 | ],
43 | controllers: [AppController],
44 | providers: [AppService, AccessControlService, ReviewService],
ERROR in ./src/app.module.ts:35:19
TS2348: Value of type 'typeof RedisStore' is not callable. Did you mean to include 'new'?
33 | RedisModule,
34 | session({
> 35 | store: new (RedisStore(session))({
| ^^^^^^^^^^^^^^^^^^^
36 | client: redisClientFactory,
37 | }),
38 | secret: 'my-secret',
app.module.ts
import { Module, Inject, NestModule, MiddlewareConsumer } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { LoggerModule } from '@app/common';
import { ConfigModule } from '@app/common';
import { DatabaseModule } from './database/database.module';
import { AuthModule } from './auth/auth.module';
import { UserModule } from './user/user.module';
import { ProductModule } from './product/product.module';
import { CategoryModule } from './category/category.module';
import { Role } from '@prisma/client';
import { AccessControlService } from '@app/common/access-control/access-control.service';
import { SearchModule } from '@app/common/elastic-search/elasticsearch.module';
import { ReviewService } from './review/review.service';
import { ReviewModule } from './review/review.module';
import { CartModule } from './cart/cart.module';
import RedisStore from 'connect-redis';
import * as session from 'express-session';
import { RedisModule } from '@app/common/redis/redis.module';
import { redisClientFactory } from '@app/common/redis/redis.provider';
@Module({
imports: [
LoggerModule,
ConfigModule,
DatabaseModule,
AuthModule,
UserModule,
ProductModule,
CategoryModule,
ReviewModule,
CartModule,
RedisModule,
session({
store: new (RedisStore(session))({
client: redisClientFactory,
}),
secret: 'my-secret',
resave: false,
saveUninitialized: false,
}),
],
controllers: [AppController],
providers: [AppService, AccessControlService, ReviewService],
})
export class AppModule {}
redis.module.ts
import { Module } from '@nestjs/common';
import { RedisRepository } from './redis.repository';
import { RedisService } from './redis.service';
import { redisClientFactory } from './redis.provider';
@Module({
providers: [RedisService, RedisRepository, redisClientFactory],
exports: [RedisService, RedisRepository, redisClientFactory],
})
export class RedisModule {}
redis.provider.ts
import { FactoryProvider } from '@nestjs/common';
import { Redis } from 'ioredis';
import { ConfigService } from '@nestjs/config';
export const redisClientFactory: FactoryProvider<Redis> = {
provide: 'RedisClient',
inject: [ConfigService],
useFactory: (configService: ConfigService) => {
const redisInstance = new Redis({
host: configService.get('REDIS_HOST'),
port: configService.get('REDIS_PORT'),
password: configService.get('REDIS_PASSWORD'),
});
redisInstance.on('error', (error) => {
console.error('Redis error', error);
});
return redisInstance;
},
};
甚至将商店内的客户端更改为RedisClient,这是在提供程序文件中声明的注入令牌
不熟悉这个库,但这对我来说看起来像是一个普通的 TypeScript 错误。
RedisStore
类是一个类,需要 new
运算符来实例化它。然而,它就像括号内的函数一样被调用。
new (RedisStore(session))
我会尝试移动
new
并查看错误是否发生变化:
(new RedisStore(session))
希望这有帮助。