title: Angular post request runs twice when calling api on node server
body: <p>I have a http post request but it gets fired twice when i call the api.</p>
  
  <p>Basically i know that node is asynchronous so might be waiting to run the sql to get the data but not sure how i can get around it. My code is as follows:</p>
  
  <pre><code>$http({ method: 'POST',
                              url: '/api/updateBooking',
                              data: JSON.stringify({'booking' : selectedItem }),
                              headers: {'Content-Type': 'application/json'}
                      }).success(function (data, status, headers, config) {
                         if (data == "true")
                         {
  
                             $scope.updateSuccess = true;
                                                       }
                      }).
                          error(function (data, status, headers, config) {
                          //hits this when unauthorized.
                          $scope.invalid = true;
                      });
  </code></pre>
  
  <p>So as soon as the above code is run it will go straight to the success and then the error but no data being returned. But immediately after it passes the data through.</p>
  
  <p>My server code is below which gets called</p>
  
  <p>a</p>
  
  <pre><code>pp.post('/api/updateBooking', function(req,res)
  {
       var booking = req.body.booking;
       var success = "false";
  
       console.log("req.booking=", booking.Date1);
  
       // just update the booking regardless as the booking is selected and is valid.
       console.log(booking);
  
       var bookingSql = 'SELECT * FROM Booking WHERE ' + booking.BookingID + '= ID ';
  
       connection.query(bookingSql, function(err,result){
          console.log(err);
          if(err) {success= false;}
          console.log('here result=', result);
          //if record found then ok to proceed
          if (result != null)
          {
              var updateBookingSQL = 'UPDATE Booking SET ? WHERE ID = ? ';
              var bookingFields  = {  Date1: booking.Date1,
                  Date1AMPM: booking.Date1AMPM,
                  Date2: moment(booking.date2).format('YYYY-MM-DD'),
                  Date2AMPM: booking.Date2AMPM,
                   }
  
  
              console.log('booking fields', bookingFields);
  
              //go and do the update using the bookingID
              connection.query(updateBookingSQL, [bookingFields, booking.BookingID], function(err,result)
              {
                  console.log(err);
                  if (err) {success="false";}
  
                  if (result != null)
                  {
                      console.log('update booking result', result);
                      if (result.changedRows == 1)
                      {
                          console.log('changed row', result.changedRows);
                          success = "true";
  
                      }
  
                  }
                  res.send(success);
                  console.log('result of update====', success);
              });
  
          }
  
       });
       console.log('success=', success) ;
  
  });
  </code></pre>
  
  <p>Any ideas would be great</p>