2016-04-11 15 views
11

Sono di fronte a un problema con il mio codice non riesco a capire che cosa è che non va su di esso .. cose interessanti è che API sta chiamando abbastanza buona, ma non la sua intenzione in success funzione orders.pay(ss($scope.ss, $scope.oo)) .success(function (data) {successo errore non è una funzione in Angularjs

angular.module('services.orders', ['users.service']) 
    .factory('orders', ['$http', 'user', '$q', function ($http, user, $q) { 
    'use strict'; 

function genericSuccess (res) { 
     return res.data.data; // yes, really. 
    } 


function pay (payment) { 
     return $http.post('v1/payment/authorize', payment) 
     .then(genericSuccess); 
    } 



orders.pay(ss($scope.ss, $scope.oo)) 
      .success(function (data) { 

    //It should called success either it should gone to error but it says 
    //Error:orders.pay(...).success is not a function 
    //can any one suggest how to solve it 

      notify.message('Thank you!'); 
      }).error(function (data) { 
      notify.message('Error: ' + data.data.message); 
     }); 
+0

Che aspetto ha la funzione 'orders'? Pubblicalo, se puoi. – mtndesign

+2

'.success' e' .error' sono deprecati, anche se questa non è la causa del tuo problema, dovresti davvero evitare di usarli comunque. usa invece '.then' e' .catch'. – ste2425

risposta

15
function genericSuccess (res) { 
    return res.data.data; // yes, really. 
} 

function pay (payment) { 
    return $http.post('v1/payment/authorize', payment).then(function(success) { 
    return genericSuccess(success); 
    }); 
} 

Prova che, se così va meglio?

5

Il problema è che .success e .error sono un wrapper, un'astrazione che solo il $http espone, non l'oggetto principale promessa.

Il problema è che la promessa restituita da $http viene estesa per avere queste proprietà aggiuntive, tuttavia le promesse successive non lo sono. Il tuo primo .then nel tuo servizio restituisce una promessa normale che non ha un metodo .success.

Questo è uno dei motivi per cui è stato deprecato, dovresti invece usare .then e .catch.

Quindi questo funzionerà:

$http.get().success().then();

ma questo non lo farà:

$http.get().then().success();

ma invece il vostro in realtà dovrebbe essere facendo:

$http.get().then().then();

Vedere il seguente fiddle che lo dimostrerà Fiddle

Speranza che abbia senso.

11

Come si può vedere nella documentazione angolare https://docs.angularjs.org/api/ng/service/$http

success e error non più sono disponibili. Se si vuole ancora utilizzare .success e .error nel codice che si può fare qualcosa di simile:

angular.module('services.orders', ['users.service']) 
    .factory('orders', ['$http', 'user', '$q', function ($http, user, $q) { 
    'use strict'; 

    function genericSuccess(res) { 
     return res.data.data; // yes, really. 
    } 


    function pay(payment) { 
     var successCallback, errorCallback; 
     function successFn(callback) { 
     if (typeof callback == 'function'){ 
      successCallback = callback; 
     } 

     return successErrorResponse; 
     } 

     function errorFn(callback) { 
     if (typeof callback == 'function') { 
      errorCallback = callback; 
     } 

     return successErrorResponse; 
     } 

     var successErrorResponse = { 
     success: successFn, 
     error: errorFn 
     }; 


     $http.post('v1/payment/authorize', payment) 
     .then(
      function (response) { 
      if(successCallback) { 
       successCallback(response) 
      } 
      }, 
      function (response) { 
      if(errorCallback) { 
       errorCallback(response) 
      } 
      }); 




     return successErrorResponse; 


    } 


    orders.pay(ss($scope.ss, $scope.oo)) 
     .success(function (data) { 

     //It should called success either it should gone to error but it says 
     //Error:orders.pay(...).success is not a function 
     //can any one suggest how to solve it 

     notify.message('Thank you!'); 
     }).error(function (data) { 
     notify.message('Error: ' + data.data.message); 
    }); 

    }]) 

ma si dovrebbe davvero essere l'adozione della nuova API angolare.

+0

ciao Daniele puoi rispondere a questo problema: https://stackoverflow.com/questions/44369082/excel-download-is-not-working-in-mean-stack-app/44373532#44373532 – Vinoth

+0

Mi chiedo se hanno altri motivi specifici per rinominare queste funzioni, oltre a semplicemente rinominarle. –

Problemi correlati