Moh*_*ala 20 get node.js express angularjs
所以在登录页面我发送凭据从angular到express通过get request.What我想做的是,如果在数据库中找到,发送响应并处理角度否则如果没有在db中找到我想快递发送错误响应并处理它角度误差响应函数,但我的代码不工作.
角度控制器:
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");
}
);
}
}
]);
Run Code Online (Sandbox Code Playgroud)
APP.js:
app.post('/api/therapist-login', therapist_controller.login);
Run Code Online (Sandbox Code Playgroud)
控制器:
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");
}
});
}
Run Code Online (Sandbox Code Playgroud)
mic*_*lem 33
在Node中,您可以使用res.status()发送错误:
return res.status(400).send({
message: 'This is an error!'
});
Run Code Online (Sandbox Code Playgroud)
在Angular中,您可以在promise响应中捕获它:
$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
// response.data.message will be "This is an error!"
console.log(response.data.message);
$scope.message = response.data.message
}
);
Run Code Online (Sandbox Code Playgroud)
Vla*_*lad 10
或者使用Error类的实例
response.status(code).send(new Error('description'));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28255 次 |
| 最近记录: |