Express return post

1

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.

    
asked by anonymous 11.10.2017 / 20:02

1 answer

1

You need to create asynchronous logic in this middleware to wait for the DB response before calling next . You can do it like this:

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.verifyUser(obj.name, function(err, rows) {
    if (rows.length > 0) next();
    else user.setUserByParams(req, next)
  })
}, function(req, res){
   // esta função corre quando o 'next' anterior for chamado 
   // e aqui o utilizador já estava ou está agora registado
   res.send('Ok!');
});

function verifyUser(name, cb) {
  con.query('SELECT * FROM TB_USER WHERE des_name like "${name}"', cb)
}

function query(sqlQry, cb) {
  connection.query(sqlQry, cb)
}

function setUserByParams(obj, cb) {
  con.query('
    INSERT INTO db_lifeapp.tb_user (
       des_name, des_email, des_endereco, num_cep, num_phone, des_password
    ) VALUES (
      '${obj.name}', '${obj.email}', '${obj.endereco}', ${obj.cep}, ${obj.phone}, '${obj.password}')', cb)
}

I think the logic of your code limits you.

So I changed my answer. What you have (in the question) is the response of this SELECT to be solved within dat function query with res.json(results) and so does not allow what you need.

    

11.10.2017 / 20:26