User
User

Reputation: 99

Service method not getting fired using .then

Below is my AngularJs code where I am trying to call TalentPoolService.search() after succussfull ProcessCriteria call, however for some reason it not hitting the TalentPool.Service. What am I doing wrong here?

       $scope.Search = function (item, SolrLabel) {
    if (item != null) {
        console.log('Item: ' + item.Key);
        console.log('SolrLabel: ' + SolrLabel);
        console.log($localStorage.message);

        var pvarrData = new Array();
        pvarrData[0] = JSON.stringify($localStorage.message);
        pvarrData[1] = item.Key;
        pvarrData[2] = SolrLabel;

            $http({
                method: 'POST',
                url: '/api/TalentPool/ProcessCriteria',
                data: JSON.stringify(pvarrData),
                headers: { 'Content-Type': 'application/json' }
            }).then(function (response) {
                console.log('ProcessCriteria Success fired');
                $localStorage.message = response.data;
                console.log(response.data);
                return response.data;
                },
                    function (response) { 
                        // failed
                        console.log('facet post error occured!');
                    }).then(

                          function () {
                              TalentPoolService.search().then(function successCallback(response1) {
                                  $scope.talentpoolist = response1.data.model;
                                  $localStorage.message = response1.data.baseCriteria;
                                  console.log('TalentPoolService.search successCallback fired');
                                  setTimeout(function () {
                                      LetterAvatar.transform();
                                  }, 20);
                              }, function errorCallback(response1) {
                                  $scope.errors = [];
                                  $scope.message = 'Unexpected Error while saving data!!' + response;
                              })
                          }
                    );
    }
}

Upvotes: 1

Views: 37

Answers (1)

Sai
Sai

Reputation: 2658

You must return data for chaining to work.

$http({
  method: 'POST',
  url: '/api/TalentPool/ProcessCriteria',
  data: JSON.stringify(pvarrData),
  headers: {
    'Content-Type': 'application/json'
  }
}).then(function(response) {
    console.log('ProcessCriteria Success fired');
    $localStorage.message = response.data;
    console.log(response.data);
    return response.data; // **return here**
  },
  function(response) {
    // failed
    console.log('facet post error occured!');
  }).then(

  function() {
    TalentPoolService.search().then(function successCallback(response1) {
      $scope.talentpoolist = response1.data.model;
      $localStorage.message = response1.data.baseCriteria;
      setTimeout(function() {
        LetterAvatar.transform();
      }, 20);
    }, function errorCallback(response1) {
      $scope.errors = [];
      $scope.message = 'Unexpected Error while saving data!!' + response;
    })
  }
);

Why because, the next then which you are using expects some data to work on. So, if you don't return it can't. So, must return data.

Upvotes: 1

Related Questions