node-cache-manager-redis-store icon indicating copy to clipboard operation
node-cache-manager-redis-store copied to clipboard

Property 'getClient' does not exist on type 'Store'

Open epozsh opened this issue 4 years ago • 5 comments

Getting this error with cache-manager with Typescript when trying to const redisClient = redisCache.store.getClient()

epozsh avatar Mar 31 '20 08:03 epozsh

no update?

daphnesmit avatar Aug 04 '22 09:08 daphnesmit

looking at @types/cache-manager-redis-store the namespace that contains the store isn't exported.

xaphod avatar Sep 06 '22 19:09 xaphod

I am still not able to getClient from store. Can someone suggest the workaround?

Heet1996 avatar Oct 10 '22 10:10 Heet1996

@Heet1996

@Module({
  imports: [
    // 连接redis
    CacheModule.registerAsync({
      isGlobal: true,
      inject: [ConfigService],
      useFactory: async (configService: ConfigService) => {
        const store = await redisStore({
          socket: {
            host: configService.get('REDIS_HOST'),
            port: +configService.get('REDIS_PORT'),
          },
          ttl: 60 * 30,
        });

        return {
          store: () => store,
        } as unknown as CacheStore;
      },
    }),
  ],
  providers: [RedisCacheService],
  exports: [RedisCacheService],
})

bihongbin3027 avatar Dec 05 '22 08:12 bihongbin3027

The issue is that the usage example from the README: https://github.com/dabroek/node-cache-manager-redis-store/blob/04d04cb5e5a472a9f8eb78c59fa1f90eb6df5839/README.md#single-store) no longer works with the latest version of "node-cache-manager":

// not awaiting here is the first issue, since caching fn from node-cache-manager was updated in v5 to be async, see https://github.com/node-cache-manager/node-cache-manager#usage-examples
const redisCache = cacheManager.caching({
    store: await redisStore(config),
})

// redisCache.store returns an object like: { store: ... }, so can only access getClient on redisCache.store.store
const redisClient = redisCache.store.getClient()
// throws error: TypeError: Cannot read properties of undefined (reading 'getClient') 

dependencies: "cache-manager": "^5.2.3" "cache-manager-redis-store": "^3.0.1"

It's caused by a breaking change in v5 of node-cache-manager described here which makes passing in a store as is currently described in the README hit the else case in the caching fn: https://github.com/node-cache-manager/node-cache-manager/blob/d9f27d7fb74556f739df1487d1b622374886d675/src/caching.ts#L69, which sets the store property on the object returned by caching to the input { store: ... } object, hence the store.store.

Indeed, downgrading to:

"cache-manager": "4.1.0" "cache-manager-redis-store": "2.0.0" as mentioned here restores the behaviour in the README. I have noticed that even using "cache-manager-redis-store": "^3.0.0" works properly (e.g. can do await redisCache.store.get('foo') even though this was mentioned to be failing in the linked issue).

I've found that if using with "cache-manager": "^5.0.0", even though the call to caching can be done in a way that is compatible with new API, the wrap function stops working, so it is probably not a good idea to use.

Probably this package requires an update to be compatible with v5 (which from what I can see is not so likely https://github.com/node-cache-manager/node-cache-manager/issues/236)

loucadufault avatar Aug 23 '23 21:08 loucadufault