0

I'm consuming a service REST, and it currently has 5025 records, but when I use the service only 1,000 records appear. What I can do to consume absolutely all records?

this is an example of my code:

  $http({
    method: 'GET',
    url: "www.exampleurl.com",  //is a example, I can not post the real url.
    timeout:5000
  }).success(function(data){
    console.log(data)
  }).error(function(response,status,headers,config) {
   console.log("problem")
  });
yavg
  • 2,761
  • 7
  • 45
  • 115
  • The service probably has a limit on the number of records. Get the next 1000 thousand records with something like `url: "www.exampleurl.com?offset=1000"`. The exact name of the parameter would depend on the API of the service. – georgeawg Apr 04 '16 at 17:15
  • @georgeawg but how I can consume all the records? – yavg Apr 04 '16 at 19:35
  • Fetch them in 1000 record chunks and put the arrays together with `Array.concat`. – georgeawg Apr 04 '16 at 20:01
  • @georgeawg what I mean is I do not know how to do to create a cycle or something to consume all the records. – yavg Apr 04 '16 at 20:04
  • Do you want to fetch the chunks in parallel or successively one after the other? – georgeawg Apr 04 '16 at 20:34
  • @georgeawg I just want to have an array absolutely all records. – yavg Apr 04 '16 at 20:39
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108215/discussion-between-yavg-and-georgeawg). – yavg Apr 04 '16 at 20:57

1 Answers1

1

Are there docs for this API that indicate how clients should handle pagination? Usually there is some sort of paging token provided. Assuming that you can make requests in batches of 1000, you can take a brute force recursive approach:

         var dataSet = [];
        function getExampleData(skip){

        return new Promise(function(fulfill,reject){
            return $http.get('http://www.example.com?skip='+skip).then(function(res){
                    return fulfill(res);
            });
        }).then(function(res){
            if(res.data) //<--or whatever will indicate there were results and we know we need to keep going
                {
                     return getExampleData(skip +1000);
                }
                //if there are no results (or whatever our stop criteria is) we return the full dataset

                return dataSet;

        });

    }
//initial call into 
return getExampleData(0).then(function(results){
console.log(results);
});
scottjustin5000
  • 1,316
  • 12
  • 10
  • It looks interesting, and I thank you. but I understand that is the parameter "callback" (getExampleData (callback)). I also want to know why this feature in a part of your code does not have the (getExampleData ()) parameter) and is the callback function ()? thank you very much – yavg Apr 04 '16 at 20:36
  • The $http service `.success` and `.error` methods have been [deprecated](https://docs.angularjs.org/api/ng/service/$http#deprecation-notice). Please don't write examples with deprecated methods. Also see [Why are Callbacks from Promise Then Methods an Anti-Pattern?](http://stackoverflow.com/questions/35660881/why-are-callbacks-from-promise-then-methods-an-anti-pattern). – georgeawg Apr 04 '16 at 20:40
  • Yeah sorry, I threw this together faster than I should have. Yeas, callback is a function. You would call like this: getExampleData(function () { console.log('complete!'); }); – scottjustin5000 Apr 04 '16 at 20:40
  • @scottjustin5000 I excuse my ignorance, but I do not understand that it serves the parameter "callback" – yavg Apr 04 '16 at 20:43
  • Hi, based on the confusion I updated to promises to get rid of the callback altogether. I don't know how the API is doing paging so that logic is fictional. With that being said, this logic will continue to call the api until there are no results (however your logic defines that). – scottjustin5000 Apr 04 '16 at 21:28
  • @scottjustin5000 I think I should be bothering hehe, but that would have stored dataset. – yavg Apr 04 '16 at 21:43
  • @scottjustin5000 apparently this works, but it becomes an infinite loop. why? – yavg Apr 04 '16 at 22:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/108220/discussion-between-scottjustin5000-and-yavg). – scottjustin5000 Apr 04 '16 at 22:16