Nest JS将动态对象从控制器传递到服务,而无需使用参数

发布于 2025-02-13 15:06:48 字数 97 浏览 2 评论 0原文

我正在使用Nest JS.I有以下方案,当我处理HTTP请求时,我需要从控制器传递contured。 我需要访问服务中的定制,而无需将其传递在道具中。他们是注入自定义变量的方法吗?

I'm using nest js.I have the following scenario where i need to pass customId from controller where i got it using interceptor when i handled the http request.
I need to access customId in service without passing it in the props. Is their a way to inject custom variables ?

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

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

发布评论

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

评论(2

滿滿的愛 2025-02-20 15:06:48

添加“定制”是请求对象req.customid应该有效。

Adding "customId" is request object req.customId should work.

今天小雨转甜 2025-02-20 15:06:48

@Injectable({scope:Scope.REQUEST})
export class IdService{
  id:string='';
  public setId(id:string){
    this.id=id
  }
}

在控制器i中创建了一个自定义,然后使用setID,然后我从UserRepository

Controller中读取ID:

this.repo.setId(body.payload.tax)

USERREREPOSIOTRY:

   const id= this.repo.id

I created an customRepository

@Injectable({scope:Scope.REQUEST})
export class IdService{
  id:string='';
  public setId(id:string){
    this.id=id
  }
}

In the controller i inject it and use setId then i read id from userRepository

controller:

this.repo.setId(body.payload.tax)

userReposiotry:

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