Bird
0
0

Why might the following NestJS controller not cache responses despite using @UseInterceptors(CacheInterceptor)?

medium📝 Debug Q7 of 15
NestJS - Interceptors
Why might the following NestJS controller not cache responses despite using @UseInterceptors(CacheInterceptor)?
@Controller('sample')
@UseInterceptors(CacheInterceptor)
export class SampleController {
  @Get()
  async fetchData() {
    return await Promise.resolve('info');
  }
}
ABecause the CacheModule is not imported in the module
BBecause the method is asynchronous
CBecause CacheInterceptor cannot be used with @Get() methods
DBecause @UseInterceptors should be applied only on methods, not controllers
Step-by-Step Solution
Solution:
  1. Step 1: Check prerequisites for caching

    CacheInterceptor requires CacheModule to be imported and configured in the module.
  2. Step 2: Analyze code

    The controller uses CacheInterceptor but likely misses CacheModule import, so caching won't work.
  3. Final Answer:

    Because the CacheModule is not imported in the module -> Option A
  4. Quick Check:

    CacheModule import is mandatory for caching [OK]
Quick Trick: CacheModule must be imported for CacheInterceptor to work [OK]
Common Mistakes:
  • Thinking async methods prevent caching
  • Believing CacheInterceptor can't be applied at controller level

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes