Challenge - 5 Problems
Reflector and Decorators Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this custom decorator usage?
Consider this NestJS custom decorator and its usage in a controller method. What will the Reflector retrieve as metadata value?
NestJS
import { SetMetadata, Reflector } from '@nestjs/common'; const Roles = (...roles: string[]) => SetMetadata('roles', roles); class SampleController { @Roles('admin', 'user') getData() { return 'data'; } } const reflector = new Reflector(); const metadata = reflector.get('roles', SampleController.prototype.getData); console.log(metadata);
Attempts:
2 left
💡 Hint
Think about what SetMetadata stores and how Reflector retrieves it.
✗ Incorrect
The custom decorator Roles uses SetMetadata to attach an array ['admin', 'user'] under the key 'roles'. Reflector.get retrieves this exact array from the method's metadata.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a custom decorator that sets metadata 'permission'?
You want to create a custom decorator named Permission that sets metadata key 'permission' with a string value. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Remember that SetMetadata returns a decorator function.
✗ Incorrect
Option C correctly returns the decorator function from SetMetadata. Option C uses Reflect.metadata which is not the NestJS way. Option C calls Reflect.defineMetadata incorrectly without target. Option C calls SetMetadata but does not return a decorator.
🔧 Debug
advanced2:00remaining
Why does Reflector.get return undefined in this code?
Given this code snippet, why does the Reflector return undefined when trying to get 'roles' metadata?
NestJS
import { SetMetadata, Reflector } from '@nestjs/common'; const Roles = (...roles: string[]) => SetMetadata('roles', roles); class UserController { @Roles('admin') getUser() { return 'user'; } } const reflector = new Reflector(); const metadata = reflector.get('roles', UserController.getUser); console.log(metadata);
Attempts:
2 left
💡 Hint
Check how methods are accessed on classes vs prototypes.
✗ Incorrect
Reflector.get expects the target method from the prototype, not the class itself. UserController.getUser is undefined because getUser is an instance method, so accessing it directly on the class fails.
🧠 Conceptual
advanced1:30remaining
What is the main purpose of the Reflector class in NestJS?
Choose the best description of what the Reflector class does in NestJS.
Attempts:
2 left
💡 Hint
Think about how metadata is accessed after decorators run.
✗ Incorrect
Reflector is a helper class that reads metadata attached by decorators at runtime. It does not apply decorators, compile code, or manage DI containers.
❓ state_output
expert2:30remaining
What is the output of this custom decorator and Reflector usage with inheritance?
Given these classes and decorators, what will the Reflector return for 'roles' metadata on ChildController.getData?
NestJS
import { SetMetadata, Reflector } from '@nestjs/common'; const Roles = (...roles: string[]) => SetMetadata('roles', roles); class ParentController { @Roles('admin') getData() { return 'parent data'; } } class ChildController extends ParentController { getData() { return 'child data'; } } const reflector = new Reflector(); const metadata = reflector.get('roles', ChildController.prototype.getData); console.log(metadata);
Attempts:
2 left
💡 Hint
Consider how metadata is attached and whether it is inherited by overridden methods.
✗ Incorrect
The ChildController overrides getData without a decorator, so no metadata is attached to its method. Reflector.get does not inherit metadata from parent methods, so it returns undefined.