trx
trx

Reputation: 2157

Chaining the $http.get AngularJS

As I am making few call to the endpoints within a function and it was causing issue with the parallel calls to the endpoints, so it was suggested in the other question to use the promise chaining. I updated my code so we can call the endpoints one after the other, so the code looks like below

 $scope.getRequest = function () {
        var url = $rootScope.BaseURL;
        var config = {
            headers: {
                'Authorization': `Basic ${$scope.key}`,
                'Prefer': 'odata.maxpagesize=2000'
            }
        };
        $http.get(url, config)
            .then(newViewRequest)
            .then(function(response){ 
                $scope.viewRequest.data = response.data;
            },
            function (response) { // failure async
                console.log("There was an error getting the request from CORE");});
   };

    var newViewRequest = function (response) {
        var url1 = $rootScope.BaseURL + `CMQ_REQUEST('${$scope.viewRequest.barcode}')`;
        if (response.data.REV_SAMPLE_CMQREQUEST.length = 0) {
            return $http.get(url1, config)
         }
        return $q.reject({ message: 'Validations didnt work' });
    };

It always sends the reject message back from the newViewRequest if response.data.REV_SAMPLE_CMQREQUEST.length = 0, if I comment it out I get the response.data is undefined.

enter image description here

Upvotes: 0

Views: 60

Answers (1)

Naga Sai A
Naga Sai A

Reputation: 10975

Update your condition to validate instead of assigning

Issue: Update if condition as below to check response.data.REV_SAMPLE_CMQREQUEST.length whether it is 0 or not with === instead of =

if (response.data.REV_SAMPLE_CMQREQUEST.length === 0) 

Upvotes: 2

Related Questions