【发布时间】: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