无法在Javascript中更新全局变量

时间:2017-04-13 16:50:18

标签: javascript node.js scope do-while

我在Node应用中更新全局变量时遇到问题。有人可以查看我的代码,让我知道出了什么问题吗?只要我的HTTP响应对象没有" next"我已经定义了一个dowhile循环来运行。定义。在循环内部,事情按预期运行并且定义了new_json.next,但while条件返回错误,因为new_json.next在那里未定义。我在Javascript上有点新鲜,所以不知何故我的变量范围已关闭,但我无法根据其他问题弄清楚如何正确地做事。

    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"));

2 个答案:

答案 0 :(得分:1)

问题是HTTPs请求是异步的,而do / while循环是同步的。在这种情况下,在将包含next的值分配给new_json变量之前,会到达while语句。

第一次到达while语句时,尚未调用回调函数。因此,new_json的值为{}(初始值)且缺少next。因此您遇到的错误。

但解决方案并未修复new_json的初始值。解决方案是删除do / while循环并继续在HTTPs请求回调中工作。

答案 1 :(得分:0)

这是我工作的代码。谢谢yeiniel。我最终只是从它自己的末尾调用函数make_pagination_request。我还做了一些与原始问题无关的其他更改,但这些更改是调试所必需的。

    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);
                    }


                });
            }
        }
相关问题