Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: store.get is not a function - NestJs, cache-manager

Tags:

nestjs

I'm implementing caching following the NestJS docs.

Create new project for this so there's no difference between docs example and this.

  1. imports CacheModule.. just like docs
import { CacheModule, Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';

@Module({
  imports: [CacheModule.register()],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

  1. Inject CacheManager..
import { CACHE_MANAGER, Controller, Get, Inject } from '@nestjs/common';
import { Cache } from 'cache-manager';

@Controller()
export class AppController {
  constructor(
    @Inject(CACHE_MANAGER) private cacheManager: Cache,
  ) {}
}
  1. And use it
  @Get()
  async getHello(): Promise<string> {
    const value: string = await this.cacheManager.get('hello');
    if (value === null) {
      await this.cacheManager.set('hello', 'cache item', 60);
      return 'no cache exists';
    }
    return value;
  }

but I get this error, I don't know why:

TypeError: store.get is not a function
    at Object.get (.../node_modules/cache-manager/src/caching.ts:88:36)
    at AppController.getHello

This is a simple job, I think.

So there's no one seems to get this error (I looked it up).

like image 273
tekuila Avatar asked Mar 19 '26 18:03

tekuila


2 Answers

I came across the same error and could not figure it out either.

node-cache-manager was only updated to version 5 within the last day. https://github.com/node-cache-manager/node-cache-manager/tags

So I updated the package.json to use version 4.x

"cache-manager": "^4.0.0",

And now the caching works as expected.

Keep an eye on the package issue queue for further updates.

like image 107
paul.linney Avatar answered Mar 22 '26 08:03

paul.linney


package.json

"dependencies": {
  "cache-manager": "^4.1.0",
  "cache-manager-redis-store": "^2.0.0"
}
"devDependencies": {
  "@types/cache-manager": "^4.0.1"
}
like image 26
mat.twg Avatar answered Mar 22 '26 08:03

mat.twg