2014-11-25 63 views
1

有下面的代碼:AngularJS無極錯誤捕獲

angular.module('app.services', []).factory('authService', [ 
'SIGN_IN_ENDPOINT', 'SIGN_OUT_ENDPOINT', '$http', '$cookieStore', function(SIGN_IN_ENDPOINT, SIGN_OUT_ENDPOINT, $http, $cookieStore) { 
    var auth; 
    auth = {}; 
    auth.signIn = function(credentials) { 
    return $http.post(SIGN_IN_ENDPOINT, { 
     user: credentials 
    }).then(function(response, status) { 
     return $cookieStore.put('user', response.data); 
    }, function(error) { 
     return console.log("Incorrect email/password"); 
    }); 
    }; 
    return auth; 
} 

這是我的認證模塊。現在,我有以下功能控制器:

angular.module('app.admin.controllers', []).controller('SignInController', [ 
    '$scope', 'authService', '$state', function($scope, authService, $state) { 
    $scope.buttonText = "Login"; 
    return $scope.login = function() { 
     $scope.buttonText = "Logging in. . ."; 
     return authService.signIn($scope.credentials).then(function(response, status) { 
     return $state.go('allPosts'); 
     }, function(err) { 
     $scope.invalidLogin = true; 
     return $scope.buttonText = "Login"; 
     }); 
    }; 
} 

的問題:如果我輸入錯誤的電子郵件/密碼,我等了2個錯誤回調 - 從第二個從第一「然後」,但我趕上第一個錯誤回調(我可以看到控制檯日誌),然後執行成功回調! (返回$ state.go('allPosts'))。爲什麼?來自服務器的響應是401錯誤。

回答

3

原因是,你在「app.services」中發現錯誤,並且不會將問題「冒泡」到更高層次。

angular.module('app.services', []).factory('authService', [ 
'SIGN_IN_ENDPOINT', 'SIGN_OUT_ENDPOINT', '$http', '$cookieStore', function(SIGN_IN_ENDPOINT, SIGN_OUT_ENDPOINT, $http, $cookieStore) { 
    var auth; 
    auth = {}; 
    auth.signIn = function(credentials) { 
    return $http.post(SIGN_IN_ENDPOINT, { 
     user: credentials 
    }).then(function(response, status) { 
     return $cookieStore.put('user', response.data); 
    }, function(error) { 
     console.log("Incorrect email/password"); 
     return $q.reject(); //here is the important one. 
    }); 
    }; 
    return auth; 
} 

或完全錯過錯誤處理程序。

auth.signIn = function(credentials) { 
     return $http.post(SIGN_IN_ENDPOINT, { 
      user: credentials 
     }).then(function(response, status) { 
      return $cookieStore.put('user', response.data); 
     }); 
     }; 

如果您發現錯誤並在錯誤中返回一個值,請遵循承諾不知道發生的錯誤。

+0

好的,謝謝。但爲什麼我不需要將成功「泡」到更高的層次呢?我應該只爲了錯誤嗎?謝謝。 – malcoauri 2014-11-25 08:44:05

+0

通過在其中一個回調中返回一個值,您基本上可以取得成功 – 2014-11-25 09:24:17

+0

您可以添加指向文檔的鏈接嗎? – malcoauri 2014-11-25 12:23:36

1

由於auth服務正在返回then函數返回的諾言,因此在第一個錯誤回調中,您需要返回被拒絕的諾言。

你能做到這樣:

auth.signIn = function(credentials) { 
    return $http.post(SIGN_IN_ENDPOINT, { 
     user: credentials 
    }).then(function(response, status) { 
     return $cookieStore.put('user', response.data); 
    }, function(error) { 
     console.log("Incorrect email/password"); 
     return $q.reject(error); // return a rejected promise; 
    }); 
    }; 

還記得注入$q爲您服務,爲這個工作。

then返回的承諾解決了成功和錯誤回調函數的返回值。 如果你做標準的回報,你將永遠着迷於成功的道路。 當您返回$q.reject時,您將返回一個最終被拒絕的承諾。請參閱$ q上的。

您也可以從服務中的錯誤回調中引發異常並獲得相同的結果。

+0

爲什麼我不需要相同的($ q.reject)成功回調? AngularJs會自動執行嗎? – malcoauri 2014-11-25 08:46:07

+0

@malcoauri檢查我更新的答案。 – Chandermani 2014-11-25 09:29:24

相關問題