title: Node.js Async | insert into postgresql database results from api
body: <p>I am quite newbie with node.js. What i am trying to achieve is the following:</p>
  
  <ol>
  <li>Connect to my postgresql database and get info of a place (id, coordinates).</li>
  <li>call a weather api and get the info of that spot using the coordinates obtained in the previous step.</li>
  <li>Insert the returned json in the database. I get 8 hourly objects, with the weather info every 3 hours (0,3,6,9,12,15,18,21). I need to iterate through this objects and the store them in 8 records in the database.</li>
  </ol>
  
  <p>I wrote the following code:</p>
  
  <pre><code>app.get('/getapi', function(req, res){
  var json_bbdd;  
   //------------ BBDD CONNECTION----------------
              var pg = require('pg'); 
              var conString = "postgres://postgres:postgres2@localhost/places";
  
              var client = new pg.Client(conString);
              client.connect(function(err) {
                if(err) {
                   console.log('could not connect to postgres');
                }
                client.query('SELECT * from places where id=3276', function(err, result) {
                  if(err) {
                   console.log('error running query'); 
                  }
                  json_bbdd=result.rows[0];
  
                  var coords = JSON.parse(json_bbdd.json).coordinates;
                  var id = json_bbdd.id;
                  var input = {
                      query: coords[1] + ',' + coords[0] ,
                      format: 'JSON',
                      fx: '',
                      callback: 'MarineWeatherCallback'
                  };
  
  
                  var url = _PremiumApiBaseURL + "marine.ashx?q=" + input.query + "&amp;format=" + input.format + "&amp;fx=" + input.fx + "&amp;key=" + _PremiumApiKey + "&amp;tide=yes";
  
                  $.ajax({
                  type: 'GET',
                  url: url,
                  async: false,
                  contentType: "application/json",
                  dataType: 'jsonp',
                  success: function (json) {
  
                      var date= json.data.weather[0].date;
  
                            for (var i=0; i &lt; 8; i++){
  
                            var hourly = json.data.weather[0].hourly[i];
                            var time= hourly.time;
  
                              client.query('INSERT into parte (id, date, time) VALUES($1, $2, $3)', [id, date, time], 
                              function(err, result) {
                                  if (err) {
                                      console.log(err);
                                  } else {
                                      console.log('row inserted: ' + id + ' ' + time);
                                  }
  
                              }); 
  
                            } // FOR
  
                      },
                      error: function (e) {
                          console.log(e.message);
                      }
                  });
  
                  client.end();
                });
              });
  });
  </code></pre>
  
  <p>The steps 1 and 2 are performed perfectly. The third step, on the other hand, does nothing and it doesn't even throw an error.</p>
  
  <p>I read in this post: <a href="https://stackoverflow.com/questions/10502121/node-postgres-will-not-insert-data-but-doesnt-throw-errors-either">node-postgres will not insert data, but doesn&#39;t throw errors either</a> that using async module could help but i have no idea how to rewrite the code. I need some help.</p>
  
  <p>Regards,
  Aitor</p>