2017-02-07 21 views
0

AuthLoginControllerコントローラからAuthServiceファクトリのログイン機能を使用しようとしています。問題は、間違った電子メールとパスワードでUser.login関数を実行すると、console.log()はfalseを返します。しかし、私は私がAngularJs promise returns undefined

Error: response is not defined 

を取得しています右のメールアドレスとパスワードを使用する場合、すべてが機能(エラー)で正常に動作しますので、私はそれを得ることはありません{}ではなく、機能(成功)と{}にもかかわらず、彼らは、両方とも非同期呼び出しの結果です。

angular 
    .module('app') 
     .factory('AuthService', ['Member', '$q', '$rootScope', '$state', function(
     User, $q, $rootScope, $state) { 
    function login(email, password) { 
    return User 
    .login({email: email, password: password}) 
    .$promise 
    .then(function(success) { 
     $rootScope.currentUser = { 
     id: response.user.id, 
     tokenId: response.id, 
     email: email, 
     firstname: response.user.firstname, 
     lastname: response.user.lastname 
     }; 
     return true; 
    }, 
    function(error) { 
     return false; 
    } 
    ); 
} return { 
     login: login 
    }; 
    }]); 

ここに私のAuthLoginControllerコントローラがあります。私は私のAuthLoginControllerからを取得するにはどうすればよい

angular 
    .module('app') 
    .controller('AuthLoginController', ['$scope', 'AuthService', '$state', 
     function($scope, AuthService, $state) { 
    $scope.user = { 
     email: '[email protected]', 
     password: 'test1' 
    }; 

    $scope.login = function() { 
     AuthService.login($scope.user.email, $scope.user.password) 
     .then(function(response) { 
      console.log(response); 
      return; 
      } 
      //go to the default state after login 
      $state.go('test'); 
     }); 
    }; 
    }]); 

? おかげ (私の悪い英語のため申し訳ありませんが)

+0

「then」を使用する約束を返す必要はありませんか? – jdmdevdotnet

+0

私は分かりません。私はAngularJsに苦労しています。私は、その約束のメソッドを呼び出すことは、新しい派生した約束を返しますが、私はどのメソッドを使用すべきか分かりません。 – ThomasF62

答えて

0
.then(function(success) { 
     $rootScope.currentUser = { 
     id: **response**.user.id, 
     tokenId: **response**.id, 
     email: email, 
     firstname: **response**.user.firstname, 
     lastname: **response**.user.lastname 
     }; 
     return true; 
    }, 

応答変数は「成功」コールバックで定義されていません。代わりに "Success"引数を使用するか、おそらくresponseに名前を変更する必要があります。

+0

私は自分自身を恥ずかしく思っています。私はこれらのコード行を読んで、私が目が見えなくなりました。この間違いはとてもばかげている。ありがとう ! – ThomasF62