I've created an Express app that is supposed to be paginating through an external API call. I've looked at this every which way and I can't figure out why the function isn't tripping the condition to break the loop. Any help would be appreciated!
Evidence for the infinite loop is the first console.log in the request callback, "I'm making a request." I had more console.logs further down in the callback function that should also always return something but it doesn't even seem to get to those.
app.post("/api/example", function(req, res) {
var username = username;
var password = password;
var container = [];
var counter = 0;
var keepGoing = true;
var makeRequest = function() {
console.log("I'm making a request");
var URL = "https://fakeapiurl.com/&page=" + counter;
request.get(URL, {
'auth': {
'user': username,
'pass': password,
'sendImmediately': true
},
'content-type': 'application/json'
}, function(error, response, data) {
var results = JSON.parse(data);
var examples = results.examples;
var numOfExamples = results.numResults;
console.log(numOfExamples);
if ((numOfExamples === 0) || (numOfExamples === jobsContainer.length - 1)) {
counter = 5;
keepGoing = false;
} else {
counter++;
for (var i = 0; i < examples.length; i++) {
container.push(examples[i]);
}
}
if (counter === 5) {
keepGoing = false;
container.sort(function(a, b) {
etc.
});
res.send(container);
}
});// end of request call
};// end of makeRequest function
while (keepGoing === true) {
makeRequest();
}
});// end of app post
Thanks!
via Carly Lucas-Melanson
No comments:
Post a Comment