Fastify reply was can not be sent

Viewed 17
export class SessionController {
  constructor(private sessionService: SessionService) {}

  @Post('login')
  async login(
    @Req() req: FastifyRequest,
    @Body() params: LoginDto,
    @Res() reply: FastifyReply,
  ) {
    params.ipAddress = req.socket.remoteAddress; /* ::1 */
    this.sessionService
      .signin(params)
      .then((user) => {
        reply.header('Authorization', `Bearer ${user.access_token}`);
        reply.code(201).send(user);
      })
      .catch((error) => {
        console.log(error instanceof HttpException);
        reply.status(error.status).send({ errors: [error.message] });
      });
  }
}

When I hit this API, I received an error which was reply was already sent, Here I did not use the reply.sent() function two times, In this nest js application I have used the frame framework under the hood, If I run this same function with express it works fine.

{"level":40,"time":1663844608468,"pid":5951,"hostname":"yavar","reqId":"req-1","err":{"type":"FastifyError","message":"Reply was already sent.","stack":"FastifyError: Reply was already sent.\n at Reply.send (/home/bennison/Documents/project/nest-lib/node_modules/fastify/lib/reply.js:118:26)\n at /home/bennison/Documents/project/nest-lib/src/user/user.controller.ts:35:15\n at processTicksAndRejections (node:internal/process/task_queues:96:5)","name":"FastifyError","code":"FST_ERR_REP_ALREADY_SENT","statusCode":500},"msg":"Reply already sent"}

If I use the async and await method instead of then catch it works fine. If anyone knows the answer, please explain to me what mistake have I made here.

0 Answers
Related