theamateurdataanalyst
theamateurdataanalyst

Reputation: 2834

Success callback not returned with $http in angular

Currently I am trying to connect my express server to MySQL database and everything seems to be going smoothly. That is, when a button is clicked postreq() is fired and I know this because app.post(...) fires its callback. The fishy part about all of this is the the success method is not console logging i.e. its callback isn't being called. I was wondering if anyone could pinpoint why. I could keep going since I am able to query my database, but the fact that the success fallback isn't being fired makes me uncomfortable.

signupctrl.js

angular.module('LiveAPP.signUp',[])
.controller('signUpCtrl', ['$scope','$http',signUpCtrl]);

function signUpCtrl($scope,$http){
  $scope.number = 100;

  $scope.postreq = $http({
                          method: "post",
                          url: "/",
                          data: {
                              user: "Junior",
                              password: "Thisispassword"
                          }
                          }).success(function(){
                            console.log("User posted to the database")
                          });
};

server.js

var express = require('express');
var app = express();
var mysql = require('mysql');

var bodyParser = require('body-parser')

app.use(express.static(__dirname + '/public'));
app.use(express.static(__dirname + '/public/views'));
app.use(express.static(__dirname + '/public/controlers'));
app.use(bodyParser())


var connection = mysql.createConnection({
    host        : 'localhost',
    port        :  3306,
    user        : 'root',
    password    : '',
    database    : 'live_users'
});


connection.connect(function(err){
if(!err) {
    console.log("Database is connected ... \n\n");  
} else {
    console.log("Error connecting database ... \n\n");  
}
});


app.post('/',function(req,res){
  connection.query('SELECT * FROM users',function(err, rows,fields){
  if (!err)
    console.log('The solution is: ', rows);
  else
    console.log('Error while performing Query.');
  }); 
})

app.listen(3000);

Upvotes: 0

Views: 54

Answers (1)

Brennan
Brennan

Reputation: 1785

You need to add a response in your query callback. Try something like this:

app.post('/',function(req,res){
  connection.query('SELECT * FROM users',function(err, rows,fields){
    if (!err){
      console.log('The solution is: ', rows);
      res.sendStatus(200);
    }else{
      console.log('Error while performing Query.');
      res.sendStatus(500);
    }
  }); 
})

Upvotes: 1

Related Questions