【发布时间】:2020-11-02 04:58:10
【问题描述】:
我有这个数组,每个数组有 8 个项目
var array_pullrequest_id=["335","328","326","323","322","314","295","291"];
var array_uniqueName=["A@A.com","B@B.com","C@C.com","D@D.com","E@E.com","F@F.com","G@G.com","H@H.com"];
我正在尝试对数组的每个索引进行发布请求:
function test2(){
var array_pullrequest_id=["335","328","326","323","322","314","295","291"];
var array_uniqueName=["A@A.com","B@B.com","C@C.com","D@D.com","E@E.com","F@F.com","G@G.com","H@H.com"];
var count = 8;
for (var i=0; i<count; i++){
var pullRequests_id = array_pullrequest_id[i];
var createdBy = array_uniqueName[i];
console.log("first index: " + i);
console.log("first console log pullRequest ID: " + pullRequests_id);
console.log("first console log Created by: " + createdBy);
var options = {
'method': 'GET',
'url': 'https://HIDEN_URL/pullRequests/'+ pullRequests_id+'/workitems',
'headers': {
'Authorization': 'Basic HIDEN_AUTH',
'Cookie': 'HIDEN_COOKIE'
}
}
request(options, function (error, response) {
console.log("second index: " + i);
console.log("second console log pullRequest ID: " + pullRequests_id);
console.log("second console log Created by: " + createdBy);
});
}
}
现在这是控制台输出:
first index: 0
first console log pullRequest: 335
first console log Created by: A@A.com
first index: 1
first console log pullRequest: 328
first console log Created by: B@B.com
first index: 2
first console log pullRequest: 326
first console log Created by: C@C.com
first index: 3
first console log pullRequest ID: 323
first console log Created by: D@D.com
first index: 4
first console log pullRequest ID: 322
first console log Created by: E@E.com
first index: 5
first console log pullRequest ID: 314
first console log Created by: F@F.com
first index: 6
first console log pullRequest ID: 295
first console log Created by: G@G.com
first index: 7
first console log pullRequest ID: 291
first console log Created by: H@H.com
second index: 8
second console log pullRequest ID: 291
second console log Created by: H@H.com
second index: 8
second console log pullRequest ID: 291
second console log Created by: H@H.com
second index: 8
second console log pullRequest ID: 291
second console log Created by: H@H.com
second index: 8
second console log pullRequest ID: 291
second console log Created by: H@H.com
second index: 8
second console log pullRequest ID: 291
second console log Created by: H@H.com
second index: 8
second console log pullRequest ID: 291
second console log Created by: H@H.com
second index: 8
second console log pullRequest ID: 291
second console log Created by: H@H.com
second index: 8
second console log pullRequest ID: 291
second console log Created by: H@H.com
现在您可以看到 第一个控制台日志 项目在计数变量索引之后正确打印,但在 for 循环请求函数 (第二个控制台日志) 内它是唯一的打印最后一个数组项,即使它在循环内,它也只是最后一个,这对我来说没有意义......
【问题讨论】:
-
request 我相信是一个异步函数,所以第一个控制台日志和第二个控制台日志没有按顺序执行。循环首先完成,然后请求函数将获取循环结束时设置的最后一个变量值,这就是为什么第二个控制台日志打印 291 和 H2H.com
-
@grandia 你推荐我在nodejs中做非异步http请求的其他方法吗?
-
我建议使用其他框架,因为 request 似乎已被弃用。我用过 axios 和 fetch,我觉得两者都很棒。为了使其同步,您可以使用 async/await。你熟悉 async/await 吗?
-
感谢您的建议,我不熟悉 async/await,但我会尝试更改为这些框架 :)!
-
当然,让我贴一个代码
标签: javascript node.js api https request