在全局配置缓存后,如 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/