【问题标题】:Having trouble updating global variable in Javascript在 Javascript 中更新全局变量时遇到问题
【发布时间】:2017-04-13 16:50:17
【问题描述】:

我在更新 Node 应用程序中的全局变量时遇到问题。有人可以查看我的代码并让我知道出了什么问题吗?只要我的 HTTP 响应对象没有定义“下一个”,我就定义了一个 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"));

【问题讨论】:

    标签: javascript node.js scope do-while


    【解决方案1】:

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

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

    但解决方案不是固定new_json 的初始值。解决方案是移除 do/while 循环并在 HTTPs 请求回调中继续工作。

    【讨论】:

    • 我早该知道是这样的。这是我刚刚开始理解的事情,它是我其他一些问题的根源。我最终只是简单地从自身末尾调用函数 make_pagination_request。
    • Javascript 代码的异步性和语言中包含的同步结构一直是许多程序员问题的重要来源。我在您的评论中看到您设法通过再次调用相同的函数来解决问题。即使它有效,我也建议您研究更好的解决方案,因为如果函数多次调用自身,您将遇到堆栈溢出问题。
    • 开个玩笑。我现在可能会保持原样,但担心该函数是否会运行多次(对于当前数据库,它应该最多只有 10 倍左右)
    【解决方案2】:

    这是我开始工作的代码。谢谢耶尼尔。我最终只是简单地从自身末尾调用函数 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);
                        }
    
    
                    });
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多