ng-csv icon indicating copy to clipboard operation
ng-csv copied to clipboard

Fetching asynchronous data

Open daniel-van-niekerk opened this issue 8 years ago • 9 comments

I've gone through the issues and nothing is ever answered in a way that makes this work for me.

Basic example:

var loadData = function() {
   var deferred = $q.defer();
   $http.get('/api/data')
       .then(function (res) {
           deferred.resolve(res.data);
        }, function (res) {
           deferred.reject();
        });
   };
   return deferred.promise;
}

This doesn't work. The file immediately gets downloaded with 0 data. Can someone please tell me if there is another way to do it for ng-csv? I looked at the lazy-load example but I'm not sure what the purpose of that is.

daniel-van-niekerk avatar Jun 01 '16 08:06 daniel-van-niekerk

I've exactly the same issue with the latest version.. I've tried returning a $promise or an array after the request has been successful.

Dossea avatar Jun 03 '16 11:06 Dossea

@daniel-van-niekerk we have actually the same issue as https://github.com/asafdav/ng-csv/issues/89 posted end of March :(

Dossea avatar Jun 03 '16 11:06 Dossea

I actually found the solution. Try this code:

var loadData = function () {
    var deferred = $q.defer();
    $http.get('/api/data')
        .then(function (res) {
            $q.when(res).then(function () {
                deferred.resolve(res.data);
             });
          }, function (res) {
              deferred.reject();
          });
     return deferred.promise;
};

Note the use of $q.when(res).then

daniel-van-niekerk avatar Jun 03 '16 12:06 daniel-van-niekerk

Did anybody solve this issue? No matter what I try the csv-file is dowloaded before my data comes back. Tried many examples but all of them failed :(

Ylvur avatar Jul 19 '16 13:07 Ylvur

I found this, facing the same question in my project, and the $q.defer() pattern in @daniel-van-niekerk 's comment solved it for me. However, during the same search I discovered (thanks to this blog) that promise-chaining can accomplish the same thing much more simply:

var loadData = function () {
    return $http.get('/api/data').then(function (res) {
        return res.data;
    });
};

I updated my own application to use that pattern, and it works perfectly.

ryan-hunter-pc avatar Jul 19 '16 22:07 ryan-hunter-pc

Thank you so much @ryan-hunter-pc. Spent many hours yesterday without any success and this mornnig it works perfectly thanks to you!

Ylvur avatar Jul 20 '16 06:07 Ylvur

I had the same issue, I was using the service like this: <button ng-csv="getArray" filename="file.csv">export</button> and the getArray is a function that returns a promise which is resolved with the data. actually I have solved this issue by setting the ng-csv directive to call the function getArray() so the button will look like this: <button ng-csv="getArray()" filename="file.csv">export</button>

SamiSammour avatar Nov 21 '16 18:11 SamiSammour

$q.when solution

richard1015 avatar Jul 31 '17 10:07 richard1015

Just to help... Here is my code using $resource

$scope.csv = function() {
  
  //Init scope defer
  $scope.itemsToExport = $q.defer()
  
  //Fetch data from inside module api
  manageModules.bigdata.query({
    ids: ids
  }).$promise.then(function(response){
  
    //When promise the result, resolve the defer()
    $q.when(response).then(function() {
      $scope.itemsToExport.resolve(response)
     })

  })

  return $scope.itemsToExport.promise

}

Html

<button type="button" ng-csv="csv()" lazy-load="true" filename="report.csv" field-separator=";">Download</button>

guicuton avatar Feb 10 '21 22:02 guicuton