0

I try to create a new user on my application. it inserts to the database when am testing on postman, this is the code below

import { Request, Response } from "express";
import logger from "../utils/logger";
import { createUser } from "../service/user.service";
import { CreateUserInput } from "../schema/user.schema";
import { omit } from "lodash";

export async function createUserHandler(
  req: Request<{}, {}, CreateUserInput["body"]>,
  res: Response
) {
  try {
    const user = await createUser(req.body);
    return res.send(user);
  } catch (error: any) {
    logger.error(error);
    return res.status(409).send(error.message);
  }
}

this is the error am getting when i use the route. On postman am gettin 404, Cannot POST /api/users

{"level":50, "time": 2023-05-15T11:27:36+01:00,"pid":false,"err":{"type":"Error","message":"Cannot set headers after they are sent to the client","stack":"Error: Cannot set headers after they are sent to the client\n    at new NodeError (node:internal/errors:371:5)\n    at ServerResponse.setHeader (node:_http_outgoing:576:11)\n    at ServerResponse.header (/[Folder directory]/backend_d1motors/node_modules/express/lib/response.js:794:10)\n    at ServerResponse.send (/[Folder directory]/backend_d1motors/node_modules/express/lib/response.js:174:12)\n    at ServerResponse.json (/[Folder directory]/backend_d1motors/node_modules/express/lib/response.js:278:15)\n    at ServerResponse.send (/[Folder directory]/backend_d1motors/node_modules/express/lib/response.js:162:21)\n    at /[Folder directory]/backend_d1motors/src/controller/user.controller.ts:13:16\n    at Generator.next (<anonymous>)\n    at fulfilled (/[Folder directory]/backend_d1motors/src/controller/user.controller.ts:5:58)\n    at processTicksAndRejections (node:internal/process/task_queues:96:5)","code":"ERR_HTTP_HEADERS_SENT"},"msg":"Cannot set headers after they are sent to the client"}
Error: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:371:5)
    at ServerResponse.setHeader (node:_http_outgoing:576:11)
    at ServerResponse.header (/[Folder directory]/backend_d1motors/node_modules/express/lib/response.js:794:10)
    at ServerResponse.send (/[Folder directory]/backend_d1motors/node_modules/express/lib/response.js:174:12)
    at /[Folder directory]/backend_d1motors/src/controller/user.controller.ts:16:28
    at Generator.next (<anonymous>)
    at fulfilled (/[Folder directory]/backend_d1motors/src/controller/user.controller.ts:5:58)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
[ERROR] 11:27:36 Error: Cannot set headers after they are sent to the client```

1 Answers1

0

The issue issue is because of having the res.send() function in try block.
When there's a condition of error, the try block will execute and when it's the time of error, it will execute the code without breaking the execution flow.

try removing res.send function from the try block and keep it outside of try-catch block.

sachin
  • 1,075
  • 1
  • 7
  • 11