Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

163
Views
Select Query in for loop using Node JS

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);
        
     });
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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')
about 4 years ago · Juan Pablo Isaza Report

0

First, I believe you should update your Query eg:

select * from stock_inward where s_no in ( "'+serial_arys+'"' ) in this way you can get all the records in only One DB hit rather than making DB data requests again and again.

second, I think let will solve your issue

let params  = req.body;
let counts  = 0;
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!