0

I have an input field based on which I have to get data from database. The input field could contain phone number or vehicle number or a card number. I have to display a table with data from database based on the input field.

I have to find what the user is entering.. if it's the phone number or vehicle number or a number. I'm using expressjs and mysql.

I have 3 queries in the server side. I have a variable responseEmpty to check if response from each of queries is empty. If response from one query is empty I set it to true and do this for all 3 queries. If response from all queries is empty I send an appropriate response.

This is my code:

app.get('/getRfidUsersbyId', function(req, res) {

    var responseEmpty = "false";
    var data = {
        "error": 1,
        "rfid": ""
    };

    var valuePassed = req.query.rCardNo;

    pool.getConnection(function(err, connection) {
        connection.query("select * from mstrrfid_users where rMobileNo='" +
            valuePassed + "'",
            function(err, rows, fields) {
                connection.release();
                if (rows != "") {
                    console.log("REsponse not empty");
                    res.json(rows);
                } else {
                    responseEmpty = "true";
                    console.log("responseEmptyy" + responseEmpty)

                }
            })
    });

    pool.getConnection(function(err, connection) {
        connection.query("select * from mstrrfid_users where rCardNo='" +
            valuePassed + "'",
            function(err, rows, fields) {
                console.log(rows + " VEHi");
                connection.release();
                if (rows != "") {
                    res.json(rows);
                }
                responseEmpty = "true";
                console.log(responseEmpty + "responseEmpty");
            })
    });


    pool.getConnection(function(err, connection) {
        connection.query("select * from mstrrfid_users where rVehicleNo='" +
            valuePassed + "'",
            function(err, rows, fields) {
                console.log(rows + " VEHi");
                connection.release();
                if (rows != "") {

                    res.json(rows);
                } else {
                    responseEmpty = "true";
                }
            })
    });


    if (responseEmpty == "true") {
        console.log("check responseEmpty");
        data["error"] = 1;
        data["rfid"] = "no details found";
        res.json(data);
    }

});

But I always get responseEmpty to be false. I don't clearly know about callback functions. But I think the code at the end to check if all 3 queries are empty is getting executed first and it doesn't enter the if.

How do I get this to work?

Edit: I tried this after marking duplicate but I still get false:

        app.get('/getRfidUsersbyId',function(req,res){


        var data={
            "error":1,
            "rfid":""
        };
         var valuePassed= req.query.rCardNo;
    var responseEmpty = false;
    queryFunc(callback);

    function queryFunc(callback){

         pool.getConnection(function(err,connection){
         connection.query("select * from mstrrfid_users where rMobileNo='"
          +valuePassed+"'",function(err,rows,fields){
              connection.release();
              if(rows !=""){
                console.log(rows +"MobileNo");
                  res.json(rows);
              }else{
                  responseEmpty = true;
                  // console.log("responseEmptyy"+ responseEmpty)

              }
          })
       }); 




          pool.getConnection(function(err,connection){

         connection.query("select * from mstrrfid_users where rCardNo='"
          +valuePassed+"'",function(err,rows,fields){
            console.log(rows +" card");
              connection.release();
              if(rows !=""){
                  res.json(rows);
              }
                  responseEmpty = true;
                  // console.log(responseEmpty +"responseEmpty");

          })
       });


          pool.getConnection(function(err,connection){
         connection.query("select * from mstrrfid_users where rVehicleNo='"
          +valuePassed+"'",function(err,rows,fields){
            console.log(rows +" VEHi");
              connection.release();
              if(rows!=""){

                  res.json(rows);
              }else{
                  responseEmpty = true;
              }
          })
       });

        callback();    
    }

    function callback(){

  console.log("callback" + responseEmpty);
      if(responseEmpty == true){
        console.log("check responseEmpty");
        data["error"]=1;
              data["rfid"]="no details found";
              res.json(data);
      }
      else{
        // res.send("Response");
      }
}

This works but I don't know if it's ok:

   function queryFunc(){

     pool.getConnection(function(err,connection){
     connection.query("select * from mstrrfid_users where rMobileNo='"
      +valuePassed+"'",function(err,rows,fields){
          connection.release();
          if(rows !=""){
            console.log(rows +"MobileNo");
              res.json(rows);
          }else{
              pool.getConnection(function(err,connection){

              connection.query("select * from mstrrfid_users where rCardNo='"
             +valuePassed+"'",function(err,rows,fields){
              console.log(rows +" card");
                connection.release();
                if(rows !=""){
                    res.json(rows);
                }
                else{
                    pool.getConnection(function(err,connection){
                    connection.query("select * from mstrrfid_users where rVehicleNo='"
                    +valuePassed+"'",function(err,rows,fields){
                      console.log(rows +" VEHi");
                        connection.release();
                        if(rows!=""){

                            res.json(rows);
                        }else{
                            data["error"]=1;
                          data["rfid"]="no details found";
                          res.json(data);


                        }
                      })
                   });

                  }
            })
           });
          }
      })
   }); 


}
Abhi
  • 1,512
  • 2
  • 22
  • 46
  • Seems __Asynchronous__ operations – Satpal Oct 17 '16 at 06:36
  • Aside from the main problem (addressed by the linked question and its answers), JavaScript has a boolean type. Just use `true` and `false`, rather than `"true"` and `"false"`. Then you can use `if (flag)` and `if (!flag)` rather than `if (flag == "true")` and `if (flag == "false")`. – T.J. Crowder Oct 17 '16 at 06:39
  • @T.J.Crowder. i've posted what i tried but i still get false. can you tell me what i'm doing wrong? – Abhi Oct 17 '16 at 09:10
  • @Satpal i've posted what i tried but i still get false. can you tell me what i'm doing wrong? – Abhi Oct 17 '16 at 09:11
  • @iLiveInAPineappleUnderTheSea: You're still trying to use the value before you have it. The call to the next step in the process must be **inside** the callback from the previous step, not *after* it. – T.J. Crowder Oct 17 '16 at 09:51
  • @T.J.Crowder. i am new to javascript. can you explain in an answer. i want all the three query functions to execute – Abhi Oct 17 '16 at 11:13
  • @T.J.Crowder. i've posted code that works. can you tell me if it's ok – Abhi Oct 18 '16 at 05:13
  • @Satpal i've posted code that works. can you tell me if it's ok – Abhi Oct 18 '16 at 08:28

0 Answers0