2016-05-25 14 views
0

約束を使用する際に問題があります。 私のコントローラはjavascriptの約束を処理します

angular.module('shoppingPad').controller('customerCtrl', customerCtrl); 
function customerCtrl($scope, customerService){ 

    $scope.addCustomer=function() { 
     alert("inside add Customer function"); 
     var customer={ 
      name: $scope.name, 
      number: $scope.number 
     }; 
     customerService.addCustomer(customer).then(function(response){ 
      console.log(response); 
     }, 
     function(error){ 
      console.log(error) 
     }); 
} 

これはこれは私が エラーとして私のコントローラでエラーを取得しています私のrestService.js

angular.module('shoppingPad').service('restService',restService); 
function restService($http){ 
    //set port number and baseUrl here 
    var port=3001; 
    var baseUrl="http://localhost:"+port; 

//generic getRequest function 


this.postRequest=function(url,data,successCallback,failureCallback){ 
    $http({ 
     method:'POST', 
     url:baseUrl+"/"+url, 
     data:data 
    }).then(function(response){ 
     successCallback(response); 
    }, 
    function(error){ 
     alert("internal server error"); 
     failureCallback(error); 

    }); 

};//end of postRequest function 
}//end of service 

である私のサービス

angular.module('shoppingPad').service('customerService', customerService); 
function customerService($q, $http,restService) { 
    //this is deferred object which will resolve or resolve the promise 
    var deferred = $q.defer(); 
    //a function to add customer data to backend used service.it takes customer object as paramater. 
    this.addCustomer = function(customer) { 
    return restService.postRequest('customers/info',customer,function(response){ 
     deferred.resolve(response.data); 
     return deferred.promise; 
    }, 
    function(error){ 
     deferred.reject(error); 
     return deferred.promise; 
    }); 
}; 

です:customerService.addCustomer( ...)は未定義です。 Iamが間違っている場合は、適切なコードで私を修正してください。

答えて

3

コードの問題は、メソッドの$http()コールから何も返さないということです。

は、以下のようにあなたの方法を変更し

this.postRequest=function(url,data){ 
    return $http({ 
      method: 'POST', 
      url: baseUrl+"/"+url, 
      data: data 
      }); 

}; 

が、この中で成功とエラーコールバックを渡すが、ちょうどこの上流の方法から約束を返すと約束を取り扱わないでください。また、この約束のすべての復帰はそれ自身の約束であるため、customerService$qを使用して新しい約束を作成する必要はありません。

this.addCustomer = function(customer) { 
    return restService.postRequest('customers/info', customer); 
} 

追加の不要な約束を必要としないこの方法は:ちょうど下のようpostRequest方法から同じ約束を返します。それはキャッチの追加の利点を持っているので

$scope.addCustomer=function() { 
    alert("inside add Customer function"); 
    var customer={ 
     name: $scope.name, 
     number: $scope.number 
    }; 

    customerService.addCustomer(customer) 
    .then(function(response){ 
    console.log(response); 
    }) 
    .catch(function(error){ 
    console.log(error) 
    }); 
} 

また、私が代わりに.thenに2番目のパラメータとしてエラーコールバックを渡すの.catchを使用している:今、あなたのコントローラであなたは以下のように返された約束を扱うことができる

成功したコールバックのエラーと返された約束のエラーと一緒に

+0

私は彼が約束をどのように扱うかを知っているとは思わない。彼が知っていれば、彼はこれを求めていないでしょう。多分あなたはそれのための例を追加する必要があります。 –

+0

@TJ Done。コントローラーにハンドラーを追加 –

関連する問題