在Nestjs上的自定义拦截器中无法使用EEFLECTOR。无法导入模块
我正在为我的Nestjs应用程序创建一个拦截器。我想将一些元数据添加到我的控制器方法中,并在拦截器中获得此值。
A进行了拦截器和自定义装饰器来添加元数据,但是当我尝试将反射器在拦截器构造函数中获取时,我会收到无法解决的错误。
我的拦截器:
@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());
//...
我的自定义装饰器:
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',
) {
// .....
使用此代码,我会收到此错误:
[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
我试图将反射器添加到.module.ts中的导入和提供商中,但没有起作用:/
我该如何解决?
谢谢你
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
或者:
Or: