I am very beginner in Nodejs. Check data already exists in mysql but, I am using for loop method to check data already exists, sql query perfectly working, I want to counts how many data doesn't exists in mysql, I initialized counts = 0 inside for loop countable but it taken outside the for loop the counts gives zero,
app.get('/stock_outward', function (req, res) {
var params = req.body;
var counts = 0;
var serial_values = "SV-K8B22490,SV-K8B22491,SV-K8B22492,SV-K8B22493,SV-K8B22494,SV-K8B22495,SV-K8B22496,SV-K8B22497,SV-K8B22498,SV-K8B22499";
var serial_arys = [];
serial_arys = serial_values.split(",");
for(var i=0;i<serial_arys.length;i )
{
connection.query('select * from stock_inward where s_no = "' serial_arys[i] '"', function (error, results, fields) {
if (error) throw error;
//console.log(results.length);
if(results.length == 0)
{
}
else{
counts ;
console.log(counts);
}
});
}
console.log(counts);
});
CodePudding user response:
Instead of doing that, do it via SQL. Like so:
SELECT COUNT(*) AS total FROM stock_inward WHERE s_no NOT IN ('SV-K8B22490','SV-K8B22491','SV-K8B22492','SV-K8B22493','SV-K8B22494','SV-K8B22495','SV-K8B22496','SV-K8B22497','SV-K8B22498','SV-K8B22499')
CodePudding user response:
First, i belive you should update your Query eg:
select * from stock_inward where s_no in ( "' serial_arys[i] '"' )
in this way you can get all the records in only One DB hit rather than making DB data request again and again.
second, I think let will solve your issue
let params = req.body;
let counts = 0;