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