0

I have this code in PHP and it works excellent, I am passing it to node (javascript) but it is giving me problems, it does not expect to terninate the conditionals and continues with the process, it gives me as a result the empty variable.


      var variable = "";

      if (typeof req.body.princp !== 'undefined') {


        if (iduser == idperprincp ) {

          var sqlfav = `SELECT * FROM tblfavo WHERE idusr = '${iduser }' ORDER BY id DESC`;
          mysqlcon.query(sqlfav, function (err, refavsql) {
            if (err) throw err;

            if (refavsql.length > 0) {
              variable = "restul 1"
            } else {
              variable = "restul 2"
            }

          })

        } else {
          variable = "restul 3"
        }

      } else {
        variable = "restul 4"
      }

      console.log(variable) // Result empty..


because it always returns the bariable empty, the same code in PHP if it works well, apparently, in node (javascript) first prints the variable and then does everything within the conditional, how do you solve that problem?, thank you very much

Luigi
  • 1
  • 1
  • 2
  • 1
    Your assumption at the bottom of the question saying it console logs before doing the logic is flawed. Without a reason to say otherwise, javascript runs procedurally. – Taplar May 05 '20 at 17:48
  • 1
    It would perhaps be more helpful to readers if you would provide a complete example of the logic. The logic in the `.....` may very well contain information about your issue. A [mcve] would also be very appreciated. – Taplar May 05 '20 at 17:49

0 Answers0