2016-03-08 21 views
5

Giriş sayfasında giriş isteğiyle ifade etmek için açısaldan kimlik bilgilerini gönderiyorum. Yapmak istediğim, veritabanında bulunursa, yanıtı gönderir ve db'de bulunmazsa açısal olarak ele alırsam, hata yanıtı göndermeyi hızlı ifade ederim ve Açısal hata yanıt fonksiyonu işlemek ama benim kodum çalışmıyor.Express/node js'de hata yanıtı nasıl gönderilir?

Eğik kontrolör:

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

Denetleyici:

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

       // 
      } 


     }); 


} 
+1

Kullanım 'res.send (404 "err açmış");' – MiTa

+0

yanıtı [ 'status_code'] = status_code; yanıtı ['message'] = status_message; yanıtı ['error_message'] = error_message; geri dönüş res.jsonp (yanıt); –

+0

Kodun hangi yönü çalışmıyor? Yanıtın durumunu alıyorsunuz, ancak hata mesajı almıyor musunuz? '$ Http' sözünün hata işleyicisi çalışmıyor. Sorun hakkında biraz açıklık getirebilirseniz, hızlı bir şekilde yanıt alacaksınız. – gnerkus

cevap

8
Düğüm hatayı göndermek için res.status() kullanabilirsiniz yılında

:

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

Vaat yanıt olarak bunu yakalamak:

$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 
     } 
    ); 
+0

Çok yardımcı, teşekkürler! –

İlgili konular