将变量设置为false不起作用

时间:2016-10-17 06:31:41

标签: javascript express

我有一个输入字段,我必须从中获取数据库中的数据。输入字段可以包含电话号码或车辆号码或卡号。我必须根据输入字段显示一个包含数据库数据的表。

我必须找到用户输入的内容..如果是电话号码或车辆号码或号码。我使用的是expressjs和mysql。

我在服务器端有3个查询。我有一个变量responseEmpty来检查每个查询的响应是否为空。如果来自一个查询的响应为空,我将其设置为true并对所有3个查询执行此操作。如果所有查询的响应都为空,我会发送适当的响应。

这是我的代码:

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);
    }

});

但我总是认为responseEmpty是假的。我不清楚地知道回调函数。但我认为最后检查所有3个查询是否为空的代码首先执行,但不会输入if

如何让它发挥作用?

修改 我在标记重复后尝试了这个,但我仍然是假的:

        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");
      }
}

这有效,但我不知道它是否可以:

   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);


                        }
                      })
                   });

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


}

0 个答案:

没有答案