JSON.parse将空数组附加到字符串参数的结尾

时间:2013-10-28 01:25:48

标签: javascript json node.js

我正在编写一个简单的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:意外的令牌[

基本上,在解析之前,它会在字符串的末尾添加额外的[]。它变成

[{json}][]

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

1 个答案:

答案 0 :(得分:3)

您在循环中发出了几个异步请求,并将它们全部附加到相同的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();
});