query inside forEach loop is running only a few times
See original GitHub issueI am inserting objects from an array of object as follows:
var sql = 'INSERT INTO my_table SET ?';
arr.forEach(item => {
con.query(sql, item, function (err, result) {
if (err) throw err;
console.log("1 record inserted");
});
})
The arr
has around 80 items but the above code only be able to insert around 45 objects or so. Why does this happen? are there any other workarounds?
Issue Analytics
- State:
- Created a year ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
running mysql_query multiple time in foreach loop [closed]
When you have a limit 1 in your query, you are limiting the results to return only 1 row from the DB. Also,...
Read more >Running a query multiple times inside foreach loop and ...
I am trying to run a query multiple times with foreach loop and storing the results in an array to use later for...
Read more >10 Most Common Mistakes That PHP Developers Make - Toptal
Common Mistake #1: Leaving dangling array references after foreach loops. Not sure how to use foreach loops in PHP? Using references in foreach...
Read more >PHP foreach Loop - W3Schools
The foreach loop works only on arrays, and is used to loop through each key/value pair in an array. Syntax. foreach ($array as...
Read more >Iteration statements -for, foreach, do, and while - Microsoft Learn
The while statement: conditionally executes its body zero or more times. At any point within the body of an iteration statement, you can...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
It’s actually opposite - single connection buffers query commands and they are inserted sequentially ( mysql protocol only allows one active command at a time in a given connection ), and in case of a pool you make inserts in parallel
Ok, got it. Thanks for this nice info. I am actually inserting it into a database from a cron job. Would it be the reason for the connection being killed??