我正在编写一个简单的 Node.JS 应用程序,它使用 GitHub API 来提取用户统计信息。我正在向 /repos/:user/:repo_name/stats/contributors
发送 GET 请求,它应该返回给我一个 JSON 字符串。这是我的功能:
function getRepoCommits(token,repos,user,callback) {
for (var i=0; i<repos.length; i++) {
var returnChunk = '';
var full_name = repos[i].full_name;
//console.log(full_name);
var options = {
host: 'api.github.com',
path: '/repos/'+full_name+'/stats/contributors',
method: 'GET',
headers: {
'Accept': 'application/json',
'Authorization': 'token '+token,
'Content-Type': 'application/x-www-form-urlencoded',
}
};
var request = https.request(options, function(res) {
//res.setEncoding('utf8');
res.on('data', function(chunk) {
returnChunk += chunk;
});
res.on('end', function(chunk) {
console.log(returnChunk);
var stats = JSON.parse(returnChunk);
console.log(stats.length);
for (var j=0;j<stats.length;j++) {
}
if (i == repos.length-1) {
//callback();
}
})
});
request.on('error',function(err) {
callback(err);
console.log(err);
});
request.end();
}
}
感兴趣的区域就在这里:
res.on('end', function(chunk) {
console.log(returnChunk);
var stats = JSON.parse(returnChunk);
console.log(stats.length);
for (var j=0;j<stats.length;j++) {
}
if (i == repos.length-1) {
//callback();
}
})
当我检查 returnChunk
的值时,它是一个有效的 JSON 字符串,格式如下:
[{json}]
但是,当函数到达 JSON.parse
时,它会抛出一个错误:
SyntaxError: Unexpected token [
基本上,它是在解析字符串之前附加一个额外的 []
到字符串的末尾。变成了
[{json}][]
我已经尝试了几个小时来弄清楚如何处理这个问题,但我似乎无法弄清楚为什么要这样做。有什么想法吗?
最佳答案
您在循环中发出多个异步请求,并将它们全部附加到同一个 returnChunk
变量,因此您的所有数据都混合在一起。
请记住,JavaScript 没有 block 作用域,只有函数作用域,所以就像将 var returnChunk = ""
放在函数的顶部一样。
一个解决方案是使用 .forEach()
而不是 for
语句,这样回调将为您提供每次迭代的新范围。
repos.forEach(function(repo, i) {
var returnChunk = '';
var full_name = repo.full_name;
//console.log(full_name);
var options = {
host: 'api.github.com',
path: '/repos/'+full_name+'/stats/contributors',
method: 'GET',
headers: {
'Accept': 'application/json',
'Authorization': 'token '+token,
'Content-Type': 'application/x-www-form-urlencoded',
}
};
var request = https.request(options, function(res) {
//res.setEncoding('utf8');
res.on('data', function(chunk) {
returnChunk += chunk;
});
res.on('end', function(chunk) {
console.log(returnChunk);
var stats = JSON.parse(returnChunk);
console.log(stats.length);
for (var j=0;j<stats.length;j++) {
}
// !!! This will be unreliable because you don't know which one will finish last.
// You could maintain a separate counter that is incremented as each "end" fires
// to make sure the `callback()` happens on the last one.
if (i == repos.length-1) {
//callback();
}
})
});
request.on('error',function(err) {
callback(err);
console.log(err);
});
request.end();
});
关于javascript - JSON.parse 将空数组附加到字符串参数的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19625884/