0

I am using Node js. I am trying to call a query within a loop. But it doesn't work as you might think. I want it to work sequentially. However, the request part is always executed last. After the loop is complete, the query request is executed. But what I want is to stack sequentially in the log.

Script

try {
  console.log('updateData');
  mssql.connect(config, function(err) {
    console.log('Connect');
    var request = new mssql.Request();
    console.log(JSON.parse(req.body.data));
    var updateObj = JSON.parse(req.body.data);
    console.log(updateObj.length);
    var queryString = "";
    console.log('updateObj :', updateObj);
    for (i = 0; i < updateObj.length; i++) {
      var selectQueryString = "SELECT * FROM tBM WHERE BM_i = " + updateObj[i].c_BM_IDs;
      console.log("Query : " + selectQueryString);
      console.log('loop I :', i);
      request.query(selectQueryString, function(err, recordset) {
        console.log("BM_i : " + recordset.recordset[0].BM_i);
        //request I is Last value of loop I +1
        console.log('request I :', i);
        //org up, org lo is error
        console.log("org up : " + updateObj[i].orgUpIDs);
        console.log("org lo : " + updateObj[i].orgLoIDs);
      });
    }
  });
} catch (err) {
  console.log(err)
}

Log

if updateObj.length = 4

Query : SELECT * FROM tBM WHERE BM_i = data
loop I : 0
Query : SELECT * FROM tBM WHERE BM_i = data
loop I : 1
Query : SELECT * FROM tBM WHERE BM_i = data
loop I : 2
Query : SELECT * FROM tBM WHERE BM_i = data
loop I : 3
BM_i : data
i : 4
Uncaught TypeError: Cannot read property 'orgUpIDs' of undefined

But I want the result like below.

like this

if updateObj.length = 4

Query : SELECT * FROM tBM WHERE BM_i = data
loop I : 0
BM_i : data
i : 0
Query : SELECT * FROM tBM WHERE BM_i = data
loop I : 1
BM_i : data
i : 1
Query : SELECT * FROM tBM WHERE BM_i = data
loop I : 2
BM_i : data
i : 2
Query : SELECT * FROM tBM WHERE BM_i = data
loop I : 3
BM_i : data
i : 3

How can I implement it?

Thanks for your answer

Tomalak
  • 332,285
  • 67
  • 532
  • 628
kudy
  • 51
  • 7
  • 3
    Queries are asynchronous. – Barmar Nov 09 '20 at 07:42
  • @Barmar I want to process the query synchronously. – kudy Nov 09 '20 at 07:43
  • Yuo need to start the next query in the callback function of the previous query, instead of using a loop. – Barmar Nov 09 '20 at 07:45
  • 3
    [Remove the callback and `.query()` will return a promise](https://www.npmjs.com/package/mssql#query-command-callback). You can then `await` it. With that said, making requests to the database in a loop is a bit suspicious. Why not create a single query that gets all of your data at once? You can then iterate through the result set in memory once. – VLAZ Nov 09 '20 at 07:45
  • 1
    Or use `async/await`. – Barmar Nov 09 '20 at 07:46
  • may be wrap the function around async function and await the request.query – Bibek Nov 09 '20 at 07:46
  • Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Darth Nov 09 '20 at 07:51

0 Answers0