使用$ http in angular返回成功回调

时间:2015-05-18 19:08:57

标签: mysql express

目前我正在尝试将我的快速服务器连接到MySQL数据库,一切似乎都很顺利。也就是说,当点击一个按钮时,postreq()被触发,我知道这是因为app.post(...)触发了它的回调。关于所有这些的可疑部分是成功方法不是控制台记录,即它的回调没有被调用。我想知道是否有人可以找出原因。我可以继续前进,因为我能够查询我的数据库,但成功后备不被解雇的事实让我感到不舒服。

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);

1 个答案:

答案 0 :(得分:1)

您需要在查询回调中添加响应。尝试这样的事情:

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);
    }
  }); 
})