如何在nestjs中运行时扫描所有装饰器值

Izw*_*ies 4 javascript node.js typescript nestjs

我必须收集出现在应用程序中不同位置的所有装饰器值作为字符串,然后在运行时将它们保存到数据库中,我不必将它们添加两次(在数据库和代码中),

我尝试过这样做,但我不知道我使用

Reflector来自 Nestjs 的 api 如下

this.reflector.getAll<string>('access', context.getHandler())
Run Code Online (Sandbox Code Playgroud)

但我无法context.getHandler()在运行时得到

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalPipes(new ValidationPipe());
   

  // Here is where i want to save

  await app.listen(3000);

}
bootstrap();
Run Code Online (Sandbox Code Playgroud)

这是我的装饰器

@HashPermission('access_value')
Run Code Online (Sandbox Code Playgroud)

请协助

Jay*_*iel 6

对于这样的事情,您要么需要使用Nest 的未记录的DiscoveryService@golevelup/nestjs-discovery东西,要么需要像Nest 包的友好包装器这样的包。然后,您可以使用诸如this.discoveryService.methodsAndControllerMethodsWithMetaAtKey获取具有该元数据的类和方法之类的方法,然后您可以在每个方法上使用反射器类来获取元数据值。