I have accionController and accionService, and my goal is to bring the actionsService stuff into actionsControllers to use it in the “data:” response but whenever I try to get something, it returns undefined in the console, this is what accionController looks like:
const getAccion = async (req, res) => {
const { id_accion } = req.params;
if (!id_accion ) {
return res.status(400).send({
status: "error",
data:{ error: "Parametro ':id_accion' no puede estar vacio" }
});
}
try {
const accion = await accionesService.getAccion(id_accion);
console.log(accion);
res.send( { status: "OK", data: accion });
} catch (error) {
res
.status(error?.status || 500)
.send({ status: "error", data: { error: error?.message || error} });
}
};
and this is how accionService looks like:
const getAccion = async (id_accion) => {
let conn;
try {
conn = await pool.getConnection();
const rows = await conn.query("SELECT * FROM accion WHERE id_accion=?", [id_accion]);
await conn.end();
if (rows.length > 0) {
const accion = rows[0];
console.log(accion);
return accion;
} else {
console.log("No se encontró ninguna fila con el ID de acción proporcionado.");
return null;
}
} catch (err) {
throw err;
} finally {
if (conn) return conn.end();
}
};
The response of console.log(accion)
; in accionesService it looks like this { id_accion: 2n, descripcion: 'prueba2' }
and the response in accionesController looks like this undefined
this is my first time working with API Rest in node.js an i relly don’t know what is causing this
I thought that with this const action = await actionsService.getAction(id_action);
it would be brought correctly, but it didn’t work, my theory is that it is a problem of the async, but I don’t know how to solve it.