Bird
0
0

You wrote this code but the guard does not run on the route as expected: ```typescript @Controller('products') @UseGuards() export class ProductsController { @Get() getAll() { return [] } } ``` What is the likely cause?

medium📝 Debug Q6 of 15
NestJS - Guards
You wrote this code but the guard does not run on the route as expected: ```typescript @Controller('products') @UseGuards() export class ProductsController { @Get() getAll() { return [] } } ``` What is the likely cause?
AThe @UseGuards decorator is missing the guard class inside the parentheses
BGuards cannot be applied at the controller level
CThe @Get decorator must come before @UseGuards
DThe guard class must be imported inside the controller file
Step-by-Step Solution
Solution:
  1. Step 1: Check @UseGuards usage

    @UseGuards() is called without any guard class, so no guard is applied.
  2. Step 2: Understand correct syntax

    You must pass the guard class inside @UseGuards(), e.g., @UseGuards(AuthGuard).
  3. Final Answer:

    The @UseGuards decorator is missing the guard class inside the parentheses -> Option A
  4. Quick Check:

    Empty @UseGuards() means no guard applied [OK]
Quick Trick: Always pass guard class inside @UseGuards() [OK]
Common Mistakes:
  • Leaving @UseGuards empty
  • Misordering decorators
  • Forgetting to import guard class

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes