camlyport
camlyport

Reputation: 109

NestJS - send Body to Response

@Post('signin')
  async signIn(@Body() account) {
    return this.appService.signIn(account);
  }

******************************
{
    "success": true,
    "message": "use cache",
    "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiJ1c2VyMkB0ZXN0LmNvbSIsImlhdCI6MTYxMDY3NjkyNSwiZXhwIjoxNjEwNzEyOTI1fQ.Tx3a0brtWYD7GQUKYQAq_48biIZCFpqEQsds2-BrINc"
}

-----------------------------------

@Post('signin')
  async signIn(@Body() account, @Res() res: Response) {
    res.send({
      result: this.appService.signIn(account)
    })
  }

******************************

{
    "result": {}
}

I'm trying to send the result as Response.

But result is {} //undefined if i using res.json() or res.send()

Cookie-parser, app.use(cookieParser()) is already done.

  1. How do i send Body using Response?

Upvotes: 2

Views: 11567

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70061

If your this.appService.login() is an async call, you need to await the response before using res.send(). Something like this should do

@Post('signin')
  async signIn(@Body() account, @Res() res: Response) {
    const result = await this.appService.signIn(account);
    res.send({
      result
    });
  }

Is there any reason you want to inject the @Res() instead of letting Nest handle it?

Upvotes: 2

Related Questions