I'm using JavaScript to request data from geotab api. I'm looking to figure out how to use a token, or version in geotab, to pull all the data beyond the limit. The issue I'm having is how to loop the feed request to pull all the data.
I tried creating a loop with python logic, thinking I can loop the requests until the current token equals the next token (this should mean it reached the end of the requests). However I found out variables don't work in JavaScript like in python:
while version != nextversion:
execute data feed request
This is the example code provided by Geotab. The example calls the data feed for Trips data in a date range and limiting it to 10 records each call. It will only return the first and second calls. What would be the best way to loop that feed until I get all the data to the toDate?
const GeotabApi = require('mg-api-js');
const fs = require('fs');
/*Authenticate*/
const authentication = {
credentials: {
database: 'database',
userName: 'username',
password: 'hunter2'
},
path: 'http://server.com/'
}
const api = new GeotabApi(authentication);
api.authenticate( success => {
console.log('Successful authentication');
}, (error) => {
console.log('Something went wrong');
});
/* The following JavaScript example shows how to call GetFeed and return a list of Trips. Note that it returns only the first 10 trips and after that another 10. */
var feed = (function(){
var version = "0000000000000000";
return {
next: function(success){
api.call("GetFeed", {
"typeName":"Trip",
"resultsLimit": 10,
"fromVersion": version,
"search": {
toDate: (new Date()).toISOString(),
fromDate: (new Date((new Date()).getTime() - (7 * 24 * 60 * 60 * 1000))).toISOString()
}
}, function(result){
version = result.toVersion;
success(result.data);
});
},
reset: function(){
version = "0000000000000000";
}
};
})();
feed.next(function(trips){
console.log("First part: ", trips);
feed.next(function(trips){
console.log("Second part: ", trips);
});
});