72

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");
      }
    });
  }
3
  • 4
    Use res.send("logged err",404); Commented Mar 8, 2016 at 10:00
  • response['status_code'] = status_code; response['message'] = status_message; response['error_message'] = error_message; return res.jsonp(response); Commented Mar 8, 2016 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. Commented Mar 8, 2016 at 10:58

2 Answers 2

133

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
        }
    );
Sign up to request clarification or add additional context in comments.

1 Comment

You can also use the json method javascript req.status(400).json({ message: "This is an invalid request" });
16

Or use instance of Error class

response.status(code).send(new Error('description'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.