2017-01-16 13 views
0

で行われた後、私はサービス内のメソッドを呼び出していますし、サービスの作業が行われたとき、それは、データをコントローラに戻っ 、さらに仕事を続けるべきコントローラでの成功を返さないが、サービスが呼び出され、データが生成される方法は、コントローラが停止していると、コンソールにエラーが返された後:角度サービスは作業がサービス

エラー:

Error: d is undefined 
sendOtp/<@http://xxxxxxx:8087/controller/resources/static/js/controller/mainController.js:71:14 
[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:14634:28 
scheduleProcessQueue/<@https://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:14650:27 
$RootScopeProvider/this.$get</[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:15916:16 
$RootScopeProvider/this.$get</[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:15727:15 
$RootScopeProvider/this.$get</[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:16024:13 
[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:10511:36 
[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:10683:7 
[email protected]://ajax.googleapis.com/ajax/libs/angularjs/1.4.4/angular.js:10624:1 

CONTROLLER:

app.controller('signController', ['$scope','signUpService','$location','$rootScope', function($scope, signUpService, $location, $rootScope) { 
$scope.OTP=''; 

function sendOtp(pNumber, countryId){  

    if(pNumber!==null){ 
    $rootScope.pNumber = pNumber; 
    signUpService.sendPhoneOtp(pNumber,countryId)  
    .then( 
     function(d) { 
      /*$location.path('/otp', false);*/ 
      $scope.OTP = d.response.result.otp; 
      console.error('OTP SENT SUCCESSFULLY'+$scope.OTP); 
      alert($scope.OTP); 

     }, 
     function(errResponse){ 
      console.error('Error while fetching OPT of NUMBER'); 
     }   
    ); 
    }else{ 
     alert("Number can not be empty"); 
    } 

} 
SERVICE INSIDE

METHOD:

function sendPhoneOtp(pNumber,countryId){ 
    var deferred = $q.defer(); 
    $http({ 
      method: 'POST', 
      url: 'https://xxxxx.com/service/verify/phone', 
      data: { 
        phone: pNumber, 
        countryId: countryId 
        } 
     }).success(function(response){ 
      deferred.resolve(response.data); 

     }).error(function(errResponse){ 
      console.error('Error while OTP'); 
      deferred.reject(errResponse); 
     }); 
return deferred.promise; 
} 
+1

読み取り(http://taoofcode.net/promise-anti-patterns/)あなただけの' $ HTTP(...)を返すことができ、それ自体約束ですTEAD –

+0

使用 'はconsole.log()'エラーメッセージを1として、 'response'をログに記録する' D 'は、レスポンスオブジェクトの構造が何であるかサービスで – Satpal

+0

チェックundefined'あなたがそこに持っている 'あります。 'response.data'は存在しますか? – Sangharsh

答えて

1

がにあなたのsendPhoneOtp機能を変更しよう:何dataプロパティが存在しない非推奨success機能に

function sendPhoneOtp(pNumber, countryId) { 
    return $http({ 
      method: 'POST', 
      url: 'https://xxxxx.com/service/verify/phone', 
      data: { 
      phone: pNumber, 
      countryId: countryId 
      } 
     }).then(function(response){ 
      return response.data; 
     }).catch(function(errResponse) { 
      console.error('Error while OTP'); 
     }); 
    } 

、また、あなたは$http以来、$qサービスを使用する必要はありません`イン; [アンチパターンを約束]について

+0

を見るために役立つだろう、私は成功が廃止されましたことを知っていませんしませんでした私は正確にあなたのコードをコピーして - ここでエラーがHMTL角度式の問題がある可能性を示唆 - undefined'の応答を読みますそれは必要に応じて、完璧に働いた – Sidharth

関連する問題