Reputation: 840
I'm creating an interceptor for my NestJs application. I want to add some metadata to my controller method and get this value in my interceptor.
A did my interceptor and my custom decorator to add metadata, but when I try to get the Reflector in my interceptor constructor I receive an error that I cannot solve.
My interceptor:
@Injectable()
export class MyCustomInterceptor implements CacheInterceptor {
private reflector: Reflector;
constructor(reflector: Reflector) {
this.reflector = reflector;
}
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const myReflectionData = this.reflector.get<string[]>('mykey', context.getHandler());
//...
My custom decorator:
import { applyDecorators, SetMetadata } from '@nestjs/common';
export const MyCustomDecorator = (listOfData: Array<string>) => applyDecorators(
SetMetadata('mykey', listOfData),
);
@MyCustomDecorator(['data1', 'data2'])
@UseInterceptors(MyCustomInterceptor)
@Get()
async listMyData(
@Query('limit') limit = 10,
@Query('skip') skip = '',
@Query('orderBy') orderBy = 'id',
@Query('sort') sort = 'DESC',
) {
// .....
With this code I receive this error:
[Nest] 10911 - 07/03/2022, 11:09:29 PM ERROR [ExceptionHandler] Nest can't resolve dependencies of the MyCustomInterceptor (?). Please make sure that the argument Reflector at index [0] is available in the CommunitiesModule context.
Potential solutions:
- If Reflector is a provider, is it part of the current MyAppModule?
- If Reflector is exported from a separate @Module, is that module imported within MyAppModule?
@Module({
imports: [ /* the Module containing Reflector */ ]
})
Error: Nest can't resolve dependencies of the MyCustomInterceptor (?). Please make sure that the argument Reflector at index [0] is available in the MyAppModule context.
Potential solutions:
- If Reflector is a provider, is it part of the current MyAppModule?
- If Reflector is exported from a separate @Module, is that module imported within MyAppModule?
@Module({
imports: [ /* the Module containing Reflector */ ]
})
at Injector.lookupComponentInParentModules (/.../node_modules/@nestjs/core/injector/injector.js:202:19)
at Injector.resolveComponentInstance (/.../node_modules/@nestjs/core/injector/injector.js:157:33)
at resolveParam (/.../node_modules/@nestjs/core/injector/injector.js:108:38)
at async Promise.all (index 0)
at Injector.resolveConstructorParams (/.../node_modules/@nestjs/core/injector/injector.js:123:27)
at Injector.loadInstance (/.../node_modules/@nestjs/core/injector/injector.js:52:9)
at Injector.loadProvider (/.../node_modules/@nestjs/core/injector/injector.js:74:9)
at async Promise.all (index 8)
at InstanceLoader.createInstancesOfProviders (/.../node_modules/@nestjs/core/injector/instance-loader.js:44:9)
at /.../node_modules/@nestjs/core/injector/instance-loader.js:29:13
I tried to add Reflector to imports and providers in my .module.ts, but didn't work :/
How can I do to solve?
Thanks for this
Upvotes: 1
Views: 1565
Reputation: 398
@Injectable()
export class MyCustomInterceptor implements CacheInterceptor {
constructor(private reflector: Reflector) {}
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const myReflectionData = this.reflector.get<string[]>('mykey', context.getHandler());
//...
Or:
@Injectable()
export class MyCustomInterceptor implements CacheInterceptor {
@Inject() private reflector: Reflector;
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const myReflectionData = this.reflector.get<string[]>('mykey', context.getHandler());
//...
Upvotes: 4