I've been working on developing an interceptor for my NestJs application. My goal is to include some metadata in my controller method and then retrieve this data in my interceptor.
So, I created my interceptor along with a custom decorator to add the necessary metadata. However, I encountered an issue when attempting to access the Reflector in my interceptor constructor, resulting in an unsolved error.
Here is my interceptor code:
@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());
//...
And here is 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',
) {
// .....
When running this code, it results in the following error message:
[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 attempted adding Reflector to both imports and providers in my .module.ts file, but unfortunately, it did not resolve the issue. How should I go about fixing this?
Any help would be greatly appreciated!