Bird
0
0

You want to create a custom decorator @Permissions that accepts multiple permissions and then read them in a guard. Which implementation correctly sets and retrieves this metadata?

hard📝 component behavior Q8 of 15
NestJS - Guards
You want to create a custom decorator @Permissions that accepts multiple permissions and then read them in a guard. Which implementation correctly sets and retrieves this metadata?
ADecorator uses <code>SetMetadata('permissions', permissionsArray)</code>; guard uses <code>this.reflector.get<string[]>('permissions', context.getHandler())</code>.
BDecorator sets metadata on the class only; guard reads from <code>context.getClass()</code>.
CDecorator stores permissions in a global variable; guard accesses it directly.
DDecorator uses environment variables; guard reads from <code>process.env</code>.
Step-by-Step Solution
Solution:
  1. Step 1: Setting metadata

    Use SetMetadata with key 'permissions' and pass the array of permissions.
  2. Step 2: Retrieving metadata

    In the guard, use reflector.get('permissions', context.getHandler()) to get the permissions set on the method.
  3. Final Answer:

    Decorator uses SetMetadata('permissions', permissionsArray); guard uses reflector.get from context.getHandler() -> Option A
  4. Quick Check:

    SetMetadata and Reflector.get with context.getHandler() is standard. [OK]
Quick Trick: SetMetadata with array; Reflector.get from handler [OK]
Common Mistakes:
  • Setting metadata only on class when method is decorated
  • Using global variables for metadata
  • Reading environment variables for permissions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NestJS Quizzes