3

我正在编写一个简单的 Node.JS 应用程序,它与 GitHub API 一起使用以提取用户统计信息。我正在发送一个 GET 请求,/repos/:user/:repo_name/stats/contributors该请求应该返回一个 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: 意外的标记 [

[]基本上,它在解析之前在字符串的末尾附加了一个额外的内容。它成为了

[{json}][]

我已经尝试了几个小时来弄清楚如何处理这个问题,但我似乎无法弄清楚它为什么会这样做。有什么想法吗?

4

1 回答 1

4

您在一个循环中发出多个异步请求,并将它们全部附加到同一个returnChunk变量,因此您的所有数据都混合在一起。

请记住,JavaScript 没有块作用域,只有函数作用域,所以就好像你放在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();
});
于 2013-10-28T01:36:50.153 回答