Nestjs全局缓存: CacheInterceptor problem

标签 nestjs

在全局配置缓存后,如 docs ,如果我在 app.module 之外使用 CacheInterceptor,它会抛出错误。

app.module.ts

const cacheConfig = {
  store: redisStore,
  host: 'localhost',
  port: 6379
}

@Module({
  imports: [
    CacheModule.register(cacheConfig),
    CustomerModule,
  ],
  providers: [
    {
      provide: APP_INTERCEPTOR,
      useClass: CacheInterceptor
    }
  ]
})
export class AppModule {}

客户.module.ts

@Module({
  imports: [TypeOrmModule.forFeature([CustomerRepository]), TypeOrmModule.forFeature([User])],
  controllers: [CustomerController]
})
export class CustomerModule {}

客户.controller.ts

@Controller('customer')
export class CustomerController {
  constructor(
    @InjectRepository(CustomerRepository) private customerRepository: CustomerRepository,
    @InjectRepository(User) private userRepository: Repository<User>
  ) {}

  @Get()
  @UseInterceptors(CacheInterceptor)
  async get(): Promise<any> {
    const user = await this.userRepository.findOne({ where: { id: 1 }, relations: ['customer'] })
    console.log(user.customer.name)
    const customer = await this.customerRepository.findOne({ where: { id: 1 }, select: ['id', 'name'] })
    return { customer: customer.name, email: user.email }
  }
}

我想在任何模块中使用 CacheInterceptor,而不需要导入每个模块的 CacheModule。

Nest 无法解析 APP_INTERCEPTOR 的依赖关系(UUID:6aa42c77-1bac-4098-b217-1b01eb268240)(?,Reflector)。请确保索引 [0] 处的参数在 CustomerModule 上下文中可用。

最佳答案

如果您有 { Provide: APP_INTERCEPTOR, useClass: CacheInterceptor },则无需在 controller< 中添加 @UseInterceptors() 装饰器。您应该让 CahceInterceptor 默认与其余设置一起工作

关于Nestjs全局缓存: CacheInterceptor problem,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58436589/

相关文章:

jestjs - 尽管在 package.json 中进行了配置,但 monorepo 中应用程序的 Nestjs e2e 测试无法通过 jest 解析从库中导入的 @app

unit-testing - 在 Nestjs 中 Jest 单元测试模拟 google oauthClient

nestjs - 使用 NestJs 和 Typeorm 启动时 e2e 测试失败

javascript - 是否可以设置 Nest.js HTTP 模块来全局使用 Promises 而不是 Observables?

node.js - 引导微服务时获取 ConfigService 的正确方法

typescript - nestJS中@UseGuards和Middleware有什么区别

typescript - Jest : I want to put test code under 'Project/test/' directory, 但发生配置错误

javascript - NestJS 中用于键值对对象的 ValidationPipe

node.js - NestJS CLI错误: Collection “@nestjs/schematics” cannot be resolved

typescript - NestJS:每个模块导入 HttpModule 的新实例