I have the following function that works fine and saves the new user data into the database:
static async create(req: Request, res: Response, next: NextFunction){
await pool.execute<IUser & RowDataPacket[]>(
"INSERT INTO users (email, password, admin) VALUES(?,?,?)",
[req.body.email, req.body.password, req.body.admin])
.then(() => {
res.status(200).json(("user created successfully"));
})
.catch(err => { console.log(err); next(); });
}
But when I try to change the following line:
await pool.execute<IUser & RowDataPacket[]>(
Like this:
const user = await pool.execute<IUser & RowDataPacket[]>(
Or change this line:
.then(() => {
To
.then((user) => { res.send(user);
In none of the cases I can't read the value of user
and it's undefined
. Is there a simple way that after I save a user into the database, I get a response with the saved user information inside the database?
EDIT: The configuration for the database looks like thie below code:
import mysql from "mysql2";
const pool = mysql.createPool(
{
host : (process.env.DB_HOST),
user : (process.env.DB_USER),
password: (process.env.DB_PASSWORD ),
database: (process.env.DB_NAME ),
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
}).promise();