将服务添加到自定义路线装饰器Nestjs

发布于 2025-01-31 01:52:09 字数 1096 浏览 1 评论 0原文

我有一个自定义的路线装饰器,该装饰器应该使用服务中的方法,但该服务返回未定义。 typeError:无法读取未定义的的属性'findcustomer'; Nestjs相当新的

//Custom Route Decorator - CustomerDecorator

export async function getCustomerFromExeContext(
  context: ExecutionContext,
): Promise<Customer> {
  let organizationService: OrganizationService;
  const req = context.switchToHttp().getRequest<Request>();

  if (!req.params.orgId)
    throw new HttpException('OrgId not found', HttpStatus.UNAUTHORIZED);

  const customer = await organizationService.findCustomer(
    parseInt(req.params.orgId),
  );
  if (!customer) {
    throw new HttpException('Customer not found', HttpStatus.NOT_FOUND);
  }
  return customer;
}

export const GetCustomer = createParamDecorator(
  (_, context: ExecutionContext) => getCustomerFromExeContext(context),
);

是使用装饰器的控制器中的路由,

//Controller

@Get(':orgId')
  get(@GetCustomer() customer: Customer) {
    return this.organizationService.get(customer.customerId);
  }

我可以在装饰函数中调用和使用该服务而无需返回undefined

I have a Custom Route Decorator which is suppose to use a method from a Service but the service returns undefined. TypeError: Cannot read property 'findCustomer' of undefined;
Fairly new to nestjs

//Custom Route Decorator - CustomerDecorator

export async function getCustomerFromExeContext(
  context: ExecutionContext,
): Promise<Customer> {
  let organizationService: OrganizationService;
  const req = context.switchToHttp().getRequest<Request>();

  if (!req.params.orgId)
    throw new HttpException('OrgId not found', HttpStatus.UNAUTHORIZED);

  const customer = await organizationService.findCustomer(
    parseInt(req.params.orgId),
  );
  if (!customer) {
    throw new HttpException('Customer not found', HttpStatus.NOT_FOUND);
  }
  return customer;
}

export const GetCustomer = createParamDecorator(
  (_, context: ExecutionContext) => getCustomerFromExeContext(context),
);

This is the Route within the controller that utilize the Decorator

//Controller

@Get(':orgId')
  get(@GetCustomer() customer: Customer) {
    return this.organizationService.get(customer.customerId);
  }

Is there a way I can call and use the service within the decorator function without returning undefined?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

还在原地等你 2025-02-07 01:52:09

错误清楚地告诉了发生了什么事

typeError:无法读取未定义的

的属性'findcustomer'

错误均值abysionserationservice是未定义的,并且您正在调用findcustomer in undefined。

这里

让组织服务:组织服务;

您创建一个未定义的变量。您需要先初始化它。

如果您内部有任何依赖性anduransionsVice,则需要注入该类对象。如果不只是new Ansuranationservice()将起作用

The error tells clearly what exactly happening

TypeError: Cannot read property 'findCustomer' of undefined

The error means organizationService is undefined and you are calling findCustomer on undefined.

Here

let organizationService: OrganizationService;

you create an undefined variable. you need to initialize it first.

If you have any dependency inside OrganizationService then you need to inject that class object. if not just new OrganizationService() will work

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