简体   繁体   中英

Having trouble updating global variable in Javascript

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.

Here's my code that I got working. Thanks yeiniel. I ended up simply calling the function, make_pagination_request, from the end of itself. I also made some other changes that weren't related to the original problem but were necessary for debugging.

    function make_pagination_request(json, create_response_data, callback, tokens, options) {        

            path = json.next + "&access_token=" + tokens[slug];    


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

            var str='';        

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


            function req_callback(response, create_response_scripts) {
                response.on('data', function(chunk) {    

                    str += chunk;
                });        

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

                    new_json=JSON.parse(str);
                    new_results = new_json.results
                    results= results.concat(new_results);
                    console.log('combined_length: ' + results.length)


                    if (new_json.next) {
                        make_pagination_request(new_json, create_response_data,create_response_scripts, tokens, options);
                    } else {
                        create_response_data(results, query, create_response_scripts);
                    }


                });
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM