【发布时间】:2019-07-24 15:15:35
【问题描述】:
我现在正在使用 GitHub APi、JavaScript 中的 node.js 和 https 模块。我需要从存储库中获取提交的总数。没有变量表示它的数量,所以我试图获取所有提交并计算它,但我需要遍历它们,因为它们是分页的
我试图从函数中的 gitHub api 获取数据并尝试保存在局部变量中,但是当请求结束时,变量为 void 。我试图将数据保存在全局变量中,在函数之前声明的变量中,试图将其保存在本地文件中。但我无法获取信息。 我也尝试在函数中返回数据,但我不能
function getCommits(repository){
let options {
host: host,
path: '/repos/'...
}
let request = https.request(options , (response) => {
let body = '';
response.on('data', (out) => {
body += out;
}
});
response.on('end', (out) => {
json = JSON.parse(body);
var i = json.length //This is the variable that I need to get out
// from the function
});
}
变量未定义或与我在开头声明的值相同
【问题讨论】:
-
How do I return the response from an asynchronous call 的部分副本。这个问题的第二部分是如何跨请求持久存储计数。
标签: javascript node.js api https