Good afternoon,
I have a problem with the POST return in express, I wanted to make a user registry using the following code:
router.post('/user', (req,res,next) =>{
var obj = {name:req.body.des_name,
email:req.body.des_email,
endereco:req.body.des_endereco,
cep:req.body.num_cep,
phone:req.body.num_phone,
password:req.body.des_password}
user.verifyUserTest(obj.name, function(err, rows) {
if (rows.length > 0){
res.send({res : "usuário já cadastrado"})
}
else{
user.setUserByParams(obj,res)
next()
}
})
}, function (obj, res) {
res.send("ok")
})
I need to get the return of VerifyUser
to do an if user exists if an error message appears, and if it does not exist, it calls the next one passing Obj as a parameter so that the other setUserByParams
function inserts the user into the bank.
The problem is that console.log(user.verifyUser(obj,res))
is returning undefined and I needed it to return the user or a full value than I sent to POST
other methods:
Midleware
function verifyUser(req,res){
return con.query('SELECT * FROM TB_USER WHERE des_name like "${req.name}"',res)
}
DataBase / Query:
function query(sqlQry,res){
connection.query(sqlQry,function (err,results,fields) {
if(err){
res.json(err)
}else{
console.log("Query Executada: "+ JSON.stringify(results))
res.json(results)
}
})
}
p>
Thanks for the help, Thanks!
Code setUsersByParams:
function setUserByParams(req,res){
return con.query('INSERT INTO db_lifeapp.tb_user (des_name,
des_email,
des_endereco,
num_cep,
num_phone,
des_password)
VALUES ('${req.name}',
'${req.email}',
'${req.endereco}',
${req.cep},
${req.phone},
'${req.password}')',res)
}
Uses the same call as the database query.