66

So in login page I am sending credentials from angular to express through get request.What I wanna do is that if found in database,send response and handle it in angular else if not found in db I want express to send error response and handle it angular error response function but my code isnt working.

Angular controller:

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

APP.js:

  app.post('/api/therapist-login', therapist_controller.login);

Controller:

  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");
      }
    });
  }
Sai Manoj
  • 3,809
  • 1
  • 14
  • 35
Mohammed Gadiwala
  • 1,923
  • 2
  • 18
  • 26
  • 4
    Use `res.send("logged err",404);` – MiTa Mar 08 '16 at 10:00
  • response['status_code'] = status_code; response['message'] = status_message; response['error_message'] = error_message; return res.jsonp(response); – Nitish Agarwal Mar 08 '16 at 10:37
  • What aspect of the code isn't working? Do you receive the response's status but no error message? Does the error handler for the `$http` promise fail to work. You'll get answers quickly if you could provide some clarity on the problem. – gnerkus Mar 08 '16 at 10:58

2 Answers2

124

In Node with ExpressJS you can use res.status() to send the error:

return res.status(400).send({
   message: 'This is an error!'
});

In Angular you can catch it in the promise response:

$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
        }
    );
michelem
  • 14,430
  • 5
  • 50
  • 66
15

Or use instance of Error class

response.status(code).send(new Error('description'));
Vlad
  • 7,997
  • 3
  • 56
  • 43