0

I need to make an http request through my angularjs service. It's working asynchronously, I know, but I can't catch my data.

var dataRifValues = [];

monitoraggioProcessiService.getDataRifList().then(function(response) {
    angular.forEach(response, function(date, key) {
        dataRifValues.push(date.dataRiferimento);
    });
});

this.getDataRifList = function() {
    var url = $rootScope.baseLink + "/processi/dataRif";
    var deferred = $q.defer();
    var list = [];
    if (list.length === 0) {
        var promise = ispCommunicationManager.returnPromise(
                          ispServiceRequest.prepareRequest(url,"GET",
                                {
                                    "Content-Type" : "application/json"
                                }, null, null));
        promise.then(function(response) { // Success callback
            list = response.data;
            deferred.resolve(response.data); // Resolve
        }, function(response) { // Error callback
            list = response;
            deferred.reject(response); // Reject
            util.defaultErrorHandler(list,null);
        });
        list = deferred.promise;
    }
    return list
 }

I'm calling the getDataRifList function. I expect a list with some datas.

It is like it's called twice. The first time the list is empty and the second time it has data, but my function is already executed.

What is the error?

EDIT

The call works, but when I need to set the filtroDataRiferimento selected property, the dataRifValues[0] is empty. The values property instead, has the values.

var filtroDataRiferimento = {

                label : "Data di riferimento",
                name : "dataRiferimento",
                type : "single",
                values : dataRifValues,
                selected : dataRifValues[0],
                flagMatch : false

            };

        $scope.filtraMonitoraggioProcessi(filtroDataRiferimento.selected);

1 Answers1

0

getDataRifList function is performing multiple activities. I believe but not sure just returning the promise object should also solve the problem instead of creating and returning the deferred object. And util.defaultErrorHandler(list,null); can be handled in the failure case of getDataRifList() call.

Tip : Checking length is not required as the declaration of variable is just above condition if (list.length === 0) {

Rajat
  • 398
  • 1
  • 5
  • 16
  • Hi, thank you for the help. I tried to use a simple $http request, but I have the same problem. Call works but values still empty. I edit my question :) – Alessandro Siverino Sep 18 '19 at 09:31