How to use mysql query in forloop with nodejs

how to run query in loop in node js
node.js mysql asynchronous query
node mysql nested queries
node mysql async/await
node js mysql select query example
node js mysql callback example
node mysql wait for result
node.js synchronous database call

I want to update database in for loop with mysql query.

 for(let i=0;i<userarray.length;i++){
     var sql = "update user set user_status=1 where id=" + userarray[i].id;
    con.query(sql, function(err, result) {
                    if (err) throw err;     
 console.log("Number of row update: " + result.affectedRows);

and the output is:
Number of row update:1
Number of row update:0
Number of row update:0
Number of row update:0

Here only one time database is updating.I found async/await is solution but i dont know how to use it.

If you are using mysql library you should use the parameter to insert the ids

const userarray = [{id:0}, {id:2}, {id:5}];
const idsList = [userarray .map(user =>];
const sql = "UPDATE user SET user_status = 1 WHERE id IN (?)";

con.query(sql, idsList, (err, result) => {
  if(err) throw err; 

JavaScript nodejs mysql with queries in a loop, The problem is that getConnection is asynchronous and that Javascript doesn't have a block scope, which means that by the time the callback  The following post is a quick look at the MySQL module for Node. There are currently two modules for connecting with MySQL – db-mysql and node-mysql. In this post we will be using node-mysql. Unlike db-mysql, you don’t need to install specialized MySQL client software to work with node-mysql. I’m assuming you have Node.js installed on

Maybe you can use the IN operator within the SQL query

let sql = 'update user set user_status = 1 where id IN (';
const userarray = [
    id: 1,
    id: 2,
    id: 3,
for(let i = 0; i < userarray.length; i++) {
  const split = i === 0 ? '' : ',';
  sql += `${split}${userarray[i].id}`;
sql += ')';

node.js sync nested mysql queries, node.js sync nested mysql queries For example, you are running five queries in for loop, the result of each loop will be appended to an array. Right after the for​  I tried using the async library and put the sql.query() in the first function and the rest of my code in the second function but it waits for the query to finish but not for the for loop. Can anyone help?

use it as :

var sql = "update user set user_status=1 where id in ("
for(let i=0;i<userarray.length;i++){
          sql +=  userarray[i].id + ","; 
          sql += userarray[i].id + ")";
          con.query(sql, function(err, result) {
                    if (err) throw err;                    

NodeJS running a promise-mysql query inside a foreach loop , NodeJS running a promise-mysql query inside a foreach loop waiting for result each time. I am trying to query a mysql database twice, the second time, multiple​  You're using a regular for loop to run assynchronous calls (you outter loop). To accomplish what you're trying to do, you have to consider those assynchronous scenarios. You could use promises or a module like async , that provides you a lot of methods to deal with assyncronous flows, like async.each() :

Node.js, MySQL and async/await, Writing asynchronous applications in Node.js can be hard to learn, but MySQL and promises, I gave an example of executing a few queries, have to introduce loops and conditions into an asynchronous code. When you are just beginning to use it, the async/await syntax seems like some dark magic. MySQL SELECT Query is used to select some of the records (with some of their properties if required) of a table. Contents. Node.js MySQL SELECT FROM query. Example to MySQL SELECT FROM query. Example to select only some of the columns. Example to use Result Object of MySQL SELECT FROM query.

Querying Data in MySQL Database from a Node.js Program, In this tutorial, you will learn how to query data from a table in the MySQL database from a node.js application using the mysql module. Once you have MySQL up and running on your computer, you can access it by using Node.js. To access a MySQL database with Node.js, you need a MySQL driver. This tutorial will use the "mysql" module, downloaded from NPM.

Node.js MySQL Result Object - Examples, When a MySQL Query is executed in Node.js, an object called Result Object is returned to the callback function. The Result Example – MySQL SELECT FROM Query – Accessing ResultSet. We can iterate for all the rows in result. Object. MySQL Get Started MySQL Create Database MySQL Create Table MySQL Insert Into MySQL Select From MySQL Where MySQL Order By MySQL Delete MySQL Drop Table MySQL Update MySQL Limit MySQL Join Node.js MongoDB

  • Looping through each user id and executing one query per user id is highly ineffidient. Construct a single query instead and execute that single query one time. Something like update user set user_status=1 where id in (userarray[0], userarray[1]...)
  • Which library are you using for the database connection ?
  • mysql library ..