0
0
NestJSframework~20 mins

Reflector and custom decorators in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Reflector and Decorators Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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);
Anull
Bundefined
C['roles']
D['admin', 'user']
Attempts:
2 left
💡 Hint
Think about what SetMetadata stores and how Reflector retrieves it.
📝 Syntax
intermediate
2: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?
Aexport const Permission = (perm: string) => Reflect.defineMetadata('permission', perm);
Bexport function Permission(perm: string) { return Reflect.metadata('permission', perm); }
Cexport const Permission = (perm: string) => SetMetadata('permission', perm);
Dexport function Permission(perm: string) { SetMetadata('permission', perm); }
Attempts:
2 left
💡 Hint
Remember that SetMetadata returns a decorator function.
🔧 Debug
advanced
2: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);
ABecause the 'roles' key is misspelled in the decorator.
BBecause UserController.getUser is undefined; method should be accessed via prototype.
CBecause Reflector requires the class instance, not the method reference.
DBecause SetMetadata does not attach metadata to methods, only classes.
Attempts:
2 left
💡 Hint
Check how methods are accessed on classes vs prototypes.
🧠 Conceptual
advanced
1:30remaining
What is the main purpose of the Reflector class in NestJS?
Choose the best description of what the Reflector class does in NestJS.
AIt reads metadata set by decorators on classes and methods at runtime.
BIt automatically applies decorators to controller methods.
CIt compiles TypeScript decorators into JavaScript functions.
DIt manages dependency injection container instances.
Attempts:
2 left
💡 Hint
Think about how metadata is accessed after decorators run.
state_output
expert
2: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);
Aundefined
Bnull
C[]
D['admin']
Attempts:
2 left
💡 Hint
Consider how metadata is attached and whether it is inherited by overridden methods.