在Nestjs上的自定义拦截器中无法使用EEFLECTOR。无法导入模块

发布于 2025-02-12 20:27:58 字数 2934 浏览 0 评论 0原文

我正在为我的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若有似无的小暗淡 2025-02-19 20:27:58
@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());
  //...

或者:

@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());
  //...
@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());
  //...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文