Desde node/express
hago el registro (signup) de usuario, los datos se guardan en la base de datos, pero muestra este error:
(node:9816) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
[1] at ServerResponse.setHeader (_http_outgoing.js:547:11)
[1] at ServerResponse.header (C:\cds-bookstore-new\server\node_modules\express\lib\response.js:771:10)
[1] at ServerResponse.send (C:\cds-bookstore-new\server\node_modules\express\lib\response.js:170:12)
[1] at ServerResponse.json (C:\cds-bookstore-new\server\node_modules\express\lib\response.js:267:15)
[1] at C:\cds-bookstore-new\server\dist\controllers\auth.controller.js:31:16
[1] at Generator.next ()
[1] at fulfilled (C:\cds-bookstore-new\server\dist\controllers\auth.controller.js:5:58)
[1] at processTicksAndRejections (internal/process/task_queues.js:93:5)
[1] (Use node --trace-warnings ...
to show where the warning was created)
[1] (node:9816) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
[1] (node:9816) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
aca mi archivo auth.controller.ts
export const signup = async (req: Request, res: Response) => {
if (!req.body.password || !req.body.registrationdDate || !req.body.email || !req.body.username) {
res.status(400).send('FALTA CONTENIDO EN EL CUERPO');
}
//recibo los datos (de un form, insomnia rest, etc..)
const { password, registrationDate, email, username } = (req.body);
console.log(password, registrationDate, email, username);
// el id_user es autonumerico en la db lo crea automaticamente
let pass = password;
const hash = await bcrypt.hash(pass, 10); // encripta el password
// insert en PostgreSQL
await pool.query('INSERT INTO users (pass, registration_date, email, username) VALUES ($1, $2, $3, $4) RETURNING id_user', [hash, registrationDate, email, username])
.then(data => {
res.status(200).send({ message: 'El usuario fue insertado en la db exitosamente' });
const idUser = JSON.stringify(data.rows[0].id_user);
console.log('El id de usuario insertado recientemente es: ' + idUser);
})
.catch(error => {
return res.status(400).send({ message: 'Error no se pudo insertar al usuario en la base de datos ' + error });
});
};
aca auth.routes.ts
router.post('api/auth/signup/user', signup);