2016-01-23 76 views
0

我想創建一個用戶身份驗證使用Flask作爲後端和AngularJS作爲前端。但我被困在這個錯誤。以下是我正在使用的Angular代碼。登錄函數成功執行,但也會引發以下錯誤。請幫我解決/解決這個問題。錯誤使用然後捕獲在angularjs

'use strict'; 

var userModule = angular.module('userModule', ['userServices']); 

userModule.controller('LoginController', ['$scope', '$location', 'userAuth', function($scope, $location, userAuth) { 

$scope.login = function() { 
    console.log("username: " + $scope.email + ", password: " + $scope.password); 
    // userAuth.login($scope.username, $scope.password); 

    // initial values 
    $scope.error = false; 
    $scope.disabled = true; 

    // call login from service 
    userAuth.login($scope.email, $scope.password) 
    // handle success 
    .then(function() { 
    console.log('I am in then function'); 
    }) 
    .catch(function() { 

    }) 
}; 

}]); 

錯誤

TypeError: userAuth.login(...).then(...).catch is not a function 
at Object.$scope.login   (http://localhost:5000/static/js/controllers/user.js:28:13) 
at http://localhost:5000/static/lib/angular/angular.js:6365:19 
at Object.Scope.$eval (http://localhost:5000/static/lib/angular/angular.js:8057:28) 
at Object.Scope.$apply (http://localhost:5000/static/lib/angular/angular.js:8137:23) 
at HTMLFormElement.<anonymous> (http://localhost:5000/static/lib/angular/angular.js:13159:11) 
at http://localhost:5000/static/lib/angular/angular.js:1992:10 
at Array.forEach (native) 
at forEach (http://localhost:5000/static/lib/angular/angular.js:130:11) 
at HTMLFormElement.eventHandler (http://localhost:5000/static/lib/angular/angular.js:1991:5) 

和我的登錄功能

function login(email, password) { 

// create a new instance of deferred 
var deferred = $q.defer(); 

// send a post request to the server 
$http.get('http://' + email + ':' + password + '@localhost:5000/api/login') 
    // handle success 
    .success(function (data, status) { 
    if(status === 200 && data.result){ 

     // print response 
     console.log('Response: ' + JSON.stringify(data)) 

     user = true; 
     deferred.resolve(); 
    } else { 

     // print response 
     console.log('Response: ' + JSON.stringify(data)) 

     user = false; 
     deferred.reject(); 
    } 
    }) 
    // handle error 
    .error(function (data) { 

    // print response 
    console.log('Response: ' + JSON.stringify(data)) 

    user = false; 
    deferred.reject(); 
    }); 

// return promise object 
return deferred.promise; 

    } 

我累了搜索,並搞清楚這個問題,但沒有發現任何東西的。不知道出了什麼問題:/ 任何幫助將不勝感激。

+0

顯示取之於'userAuth.login()返回'。要麼它不是一個承諾,或者你可能有一箇舊版本的角度,不包括'catch'方法 – charlietfl

+0

我認爲你可以刪除.catch()並可以添加第二個錯誤回調然後 –

+0

我已經更新了我的角度版本。我已經刪除了catch子句,這次我沒有錯誤。謝謝。但它打印'錯誤未定義',這意味着我有一些東西在我的登錄功能中斷了。請看看它。我在我的文章中包含了我的登錄功能。 –

回答

0

試試這個

userAuth.login($scope.email, $scope.password) 
.then(function() { 
    console.log('I am in then function'); 
}, 
function(error){ 
    console.log('Error', error); 
}) 
+0

謝謝,工作正常(y) –