Having problems updating the global variable in Javascript

advertisements

I'm having trouble updating a global variable in my Node app. Can someone look at my code and let me know what's going wrong? I've defined a dowhile loop to run as long as my HTTP response object does not have "next" defined. Inside the loop, things are running as expected and new_json.next is defined, but the while condition is returning an error because new_json.next is undefined there. I'm a little new at Javascript, so somehow my variable scope is off, but I can't figure out how to do things correctly based on other questions.

    function make_pagination_request(json, create_response_data) {    

        var new_json={};
        do {    

            var options = {
                 host: slug + '.nationbuilder.com',  //should be replaced to be site-agnostic
                 path: json.next,
                 method: "GET",
                 json: true,
                 headers: {
                     "content-type": "application/json",
                     "accept": "application/json"
                 },
            }    

            var req = https.get(options, req_callback);    

            function req_callback(response) {
                response.on('data', function(chunk) {
                    str += chunk;
                });    

                response.on('end', function(new_json) {    

                    new_json=JSON.parse(str);
                    new_results = new_json.results
                    results= results.concat(new_results);
                    console.log("combinedlength: " + results.length)
                    console.log(new_json.next);

                });
            }
        } while (new_json.next.includes("api"));


The problem is that the HTTPs request is asynchronous while the do/while cycle is synchronous. In this case the while statement is reached before a value containing next is assigned to the new_json variable.

When the while statement is reached the first time the callback function hasn't been called yet. Therefore the value of new_json is {} (the initial value) and it lack next. Therefore the error you are experiencing.

But the solution is not fixing the initial value of new_json. The solution is to remove the do/while loop and continue work inside the HTTPs request callback.