0

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!

Looks like I can't break from the last loop, calling the makeRequest function. 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
10
  • Have you tried printing your counter? does it increment properly? Commented Jun 7, 2017 at 22:56
  • did this console.log(numOfExamples); fire ? Commented Jun 7, 2017 at 23:03
  • @Stephen L Counter does not increment properly, perhaps this is the real reason. Is there anything wrong with how I've told it to increment? Commented Jun 7, 2017 at 23:10
  • @Abdoutelb Printing 'console.log(numOfExamples)' never fires. Commented Jun 7, 2017 at 23:11
  • Is it getting incremented to 5? Commented Jun 7, 2017 at 23:13

1 Answer 1

1

This will never work like you would expect, you're firing async requests inside a sync while loop. So at the time the first request is trying to get the data, you're firing the same request again, so your first request gets canceled. This goes like forever. You should fire the next request inside the success callback of the previous request, so it gets fired after the previous one resolves.

Something like that:

app.post("/api/example", function(req, res) {
var username = username;
var password = password;
var container = [];
var maxPages = 5;
var makeRequest = function(page) {
    console.log("I'm making a request");
    var URL = "https://fakeapiurl.com/&page=" + page; 
    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;
            var lastPageReached = (numOfExamples === 0 || numOfExamples === jobsContainer.length - 1);
            lastPageReached = lastPageReached  && page < maxPages;

            if (lastPageReached) {
                container.sort(function(a, b) {
                    etc.
                });
                res.send(container); 
            } else {
                container = container.concat(...examples);
                makeRequest(page + 1);
            } 
        });
    };
  makeRequest(0);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.