所以在登录页面中,我通过get请求从angular向express发送凭据.我想做的是,如果在数据库中找到,发送响应并在angular中处理它,否则如果在db中找不到,我希望express发送错误响应并处理它angular error response函数,但我的代码不起作用.
Angular 控制器:
myapp.controller('therapist_login_controller', ['$scope', '$localStorage', '$http',
function($scope, $localStorage, $http) {
$scope.login = function() {
console.log($scope.username + $scope.password);
var data = {
userid: $scope.username,
password: $scope.password
};
console.log(data);
$http.post('/api/therapist-login', data)
.then(
function(response) {
// success callback
console.log("posted successfully");
$scope.message = "Login succesful";
},
function(response) {
// failure callback,handle error here
$scope.message = "Invalid username or password"
console.log("error");
}
);
}
}
]);
应用程序.js:
app.post('/api/therapist-login', therapist_controller.login);
控制器:
module.exports.login = function(req, res) {
var userid = req.body.userid;
var password = req.body.password;
console.log(userid + password);
Credentials.findOne({
'userid': [userid],
'password': [password]
}, function(err, user) {
if (!user) {
console.log("logged err");
res.status(404); //Send error response here
enter code here
} else {
console.log("login in");
}
});
}