connect-redis icon indicating copy to clipboard operation
connect-redis copied to clipboard

Type 'RequestHandler<ParamsDictionary, any, any, ParsedQs, Record<string, any>>' is not assignable to type 'Type<any> | DynamicModule | Promise<DynamicModule> | ForwardReference<any>'.

Open Abdulberk opened this issue 1 year ago • 4 comments

I'm trying to implement express-session package along with connect-redis for session storage in my NestJS backend, but i got such an error as follows. I even changed the client inside the store to RedisClient, which is injection token declared in provider file, but still no luck, where im mistaken exactly?

error message:

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

Abdulberk avatar Apr 21 '24 12:04 Abdulberk

In app.module.ts, what happens if you replace:

session({
  store: new (RedisStore(session))({
    client: redisClientFactory,
  }),
  secret: 'my-secret',
  resave: false,
  saveUninitialized: false,
}),

With:

session({
  store: new RedisStore({
    client: redisClientFactory,
  }),
  secret: 'my-secret',
  resave: false,
  saveUninitialized: false,
}),

wavded avatar Apr 22 '24 14:04 wavded

In app.module.ts, what happens if you replace:

session({
  store: new (RedisStore(session))({
    client: redisClientFactory,
  }),
  secret: 'my-secret',
  resave: false,
  saveUninitialized: false,
}),

With:

session({
  store: new RedisStore({
    client: redisClientFactory,
  }),
  secret: 'my-secret',
  resave: false,
  saveUninitialized: false,
}),

still same error plus the one below:

[Nest] 6292  - 22.04.2024 17:48:23   ERROR [ExceptionHandler] Cannot read properties of undefined (reading 'session')
TypeError: Cannot read properties of undefined (reading 'session')

Abdulberk avatar Apr 22 '24 15:04 Abdulberk

OK, I'm unfamiliar with NestJS so I'm not sure what else is going on under the hood, what version of connect-redis are you running?

wavded avatar Apr 22 '24 17:04 wavded

OK, I'm unfamiliar with NestJS so I'm not sure what else is going on under the hood, what version of connect-redis are you running?

thanks though, appreciated.. I have never implemeted session storage on NestJS before, so i'm also trying to understand whats really going on under the hood with those packages, on the other hand, I perfectly got Redis implementation up and running for other stuffs like shopping cart and caching some static resources, im pretty sure nothing is wrong with my client config... now I just need to find a way to do that so that I can keep track of anonymous users' (not authenticated) shopping cart data in session stores in Redis side. "connect-redis": "^7.1.1",

Abdulberk avatar Apr 22 '24 19:04 Abdulberk

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.

github-actions[bot] avatar May 23 '24 00:05 github-actions[bot]

This issue was closed because it has been stalled for 5 days with no activity.

github-actions[bot] avatar May 28 '24 00:05 github-actions[bot]