skip to Main Content

I am trying to implement redis using the packages : "CacheModule" and "cache-manager-redis-store". The latter brings me a type error when assigning it to the store property of the register method of CacheModule.

code where the error occurs:

`

import { Module, CacheModule } from "@nestjs/common";
import { AuthModule } from "./auth/auth.module";
import { MongooseModule } from "@nestjs/mongoose";
import { EnvConfiguration } from "./config/env.config";
import { ConfigModule } from "@nestjs/config";
import { redisStore } from "cache-manager-redis-store";
@Module({
  imports: [
    ConfigModule.forRoot({
      load: [EnvConfiguration],
    }),
    MongooseModule.forRoot(process.env.MONGODB),
    AuthModule,
    CacheModule.register({
      store: redisStore, <--- HERE THE ERROR HAPPENS
      host: "localhost",
      port: 6379,
    }),
  ],
  controllers: [],
  providers: [],
})
export class AppModule {}

`

ERROR:
`

(property) store: (string | CacheStoreFactory | CacheStore) & ((config: RedisClientOptions<RedisModules, RedisFunctions, RedisScripts> & Config) => Promise<...>)
Cache manager. The default value is 'memory' (memory storage). See Different Stores for more information.

Type '(config: RedisClientOptions<RedisModules, RedisFunctions, RedisScripts> & Config) => Promise<RedisStore>' cannot be assigned to type '(string | CacheStoreFactory | CacheStore) & ((config: RedisClientOptions<RedisModules, RedisFunctions, RedisScripts > & Configuration) => Promise<...>)'.
  Type '(config: RedisClientOptions<RedisModules, RedisFunctions, RedisScripts> & Config) => Promise<RedisStore>' cannot be assigned to type 'string & ((config: RedisClientOptions<RedisModules, RedisFunctions, RedisScripts> & Config) => Promise<...>)'.
    Type '(config: RedisClientOptions<RedisModules, RedisFunctions, RedisScripts> & Config) => Promise<RedisStore>' cannot be assigned to type 'string'.ts(2322)

`

I want to be able to implement redis with nestjs, solving the described problem or with another way of implementing it that works correctly.

3

Answers


  1. Change this:

    import { redisStore } from "cache-manager-redis-store";
    

    to this:

    import redisStore from "cache-manager-redis-store";
    

    or this:

    import * as redisStore from "cache-manager-redis-store";
    

    or this:

    // eslint-disable-next-line @typescript-eslint/no-var-requires
    const redisStore = require("cache-manager-redis-store");
    
    Login or Signup to reply.
  2. The method of importing redisStore is the problem. The location pointed out as the issue is where the error is being generated.

    From Nest JS Docs

    import type { ClientOpts } from 'redis';
    import * as redisStore from 'cache-manager-redis-store';
    import { CacheModule, Module } from '@nestjs/common';
    import { AppController } from './app.controller';
    
    @Module({
      imports: [
        CacheModule.register<ClientOpts>({
          store: redisStore,
    
          // Store-specific configuration:
          host: 'localhost',
          port: 6379,
        }),
      ],
      controllers: [AppController],
    })
    export class AppModule {}
    
    Login or Signup to reply.
  3. I think you should check out this github link
    https://github.com/dabroek/node-cache-manager-redis-store/issues/40

    To sum up for you, they will use cache-manager-redis-yet instead of cache-manager-redis-store

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search