title: NodeJs MySql multiple update
body: <p>I have a method in NodeJs using Express framework, in which I am <strong>iterating an array and making an update in a Mysql DB</strong>,<br>
  The Code receives a Connection object and a Post Body,<br> 
  The Post Request Body is an Array of objects, of the data to be saved in the DB,<br>
  I am <strong>trying to loop the objects one by one and save them in the DB using an Update Query</strong>.<br></p>
  
  <p>Now the strange part is, <strong>the code works only if it gets called twice immediately</strong>,<br>
  Ie. On testing I found out, <strong>I have to make the API request twice in order for the code to save the data.</strong><br></p>
  
  <p><strong>I get the following error on the first API call -</strong><br></p>
  
  <pre><code>Error Code: 1205. Lock wait timeout exceeded; try restarting transaction
  </code></pre>
  
  <p>It's a simple Update call, I checked the MySql processes and there was no deadlock,<br></p>
  
  <pre><code>SHOW FULL PROCESSLIST;
  </code></pre>
  
  <p>But the same code work's on the immediate 2nd API call.</p>
  
  <pre><code>let updateByDonationId = async (conn, requestBody, callback) =&gt; {
  
      let donations = [];
      donations = requestBody.donations;
  
      //for(let i in donations){
      async.each(donations, function(singleDonation, callback) {
          //let params = donations[i];
          let params = singleDonation;
  
          let sqlData = []
          let columns = "";
  
          if(params.current_location != null){
              sqlData.push(params.current_location);
              columns += "`current_location` = ?,";
          }
          if(params.destination_location != null){
              sqlData.push(params.destination_location);
              columns += "`destination_location` = ?,";
          }
  
          if(columns != ''){
              columns = columns.substring(0,columns.length-1);
  
              let sqlQuery = 'UPDATE donation_data SET '+columns
              +' WHERE donation_id = "' + params.donation_id + '"';
  
              conn.query(sqlQuery, sqlData, function (err, result) {
                  logger.info(METHOD_TAG, this.sql);
                  if (err) {
                      logger.error(METHOD_TAG, err);
                      return callback(err, false);
                  }
              })
          }
          else{
              return callback(null, false);
          }
          columns = "";
          sqlData = [];
      },
      function(err, results) {
          if (err) {
              logger.error(METHOD_TAG, err);
              return callback(err, false);
          }
          else{
             return callback(null, true);
          }
      });
      //return callback(null, true);
  
  } // END
  </code></pre>
  
  <p>Also referring the following, i guess he was getting an ER_LOCK_WAIT_TIMEOUT for weird reason as well -<br>
  <a href="https://stackoverflow.com/questions/40378350/nodejs-mysql-using-connection-pool-leads-to-deadlock-tables">NodeJS + mysql: using connection pool leads to deadlock tables</a></p>
  
  <p><br><strong>The issue seems to be with the Non blocking Async nature of Node as rightly pointed out</strong>
  <br><strong>Can anyone help with a correct code?</strong></p>