使用异步

时间:2015-05-10 09:32:38

标签: node.js api asynchronous github

我在下面的代码中尝试了一个非常复杂的计算。我试图使用api https://api.github.com/repos/marklogic/java-client-api/issues?page=1&per_page=10从给定项目中的github获取错误列表。从错误列表中我试图解决每个问题'来自相应端点的对应eventscomments例如https://api.github.com/repos/marklogic/java-client-api/issues/291/eventshttps://api.github.com/repos/marklogic/java-client-api/issues/291/comments。 我正在使用async库。我使用waterfall函数和parallel函数为每个错误返回一个合并的JSON,以便每个问题都有评论,&每个问题的相同响应中的事件。问题是它抛出Can't set headers after they are sent错误&它指向第2行,我理解错误是什么,但我无法弄清楚如何解决它,因为注释掉任何违规行会导致请求挂起,因为服务器没有发送响应。请帮忙!提前致谢

exports.listGitHubBugs = function(req, res) {
    var _page = req.query.page || 1;
    var _per_page = req.query.per_page || 25;
    var finalResult = []
        //console.log('url:', 'https://api.github.com/repos/marklogic/' + req.query.project + '/issues?page=' + _page + '&per_page=' + _per_page);
    var options = {
        url: 'https://api.github.com/repos/marklogic/' + req.query.project + '/issues?page=' + _page + '&per_page=' + _per_page,
        headers: {
            'User-Agent': req.query.project
        },
        auth: githubAuth
    };
    request(options, function(error, response, body) {
        if (error) {
            console.log(error);
            res.send(error);
        }

        if (!error && response.statusCode === 200) {
            var issues = JSON.parse(response.body)

            async.waterfall([
                 // get comments & events for all bugs and then send the response
                function(callback) {
                    issues.forEach(function(issue) {
                          // for each bug, get comments and events
                        async.parallel([

                            function(parallelCallback) {
                                var options = {
                                    url: issue.events_url,
                                    headers: {
                                        'User-Agent': getProjectNameFromURL(issue.events_url)
                                    },
                                    auth: githubAuth
                                };
                                request(options, function(error, response, body) {
                                    if (error) {
                                        console.log('ERROR', error);
                                        parallelCallback(error)
                                    }
                                    if (!error && response.statusCode === 200) {
                                        // console.log('events:', body);
                                        parallelCallback(null, body)

                                    }


                                })
                            },
                            function(parallelCallback) {
                                var options = {
                                    url: issue.comments_url,
                                    headers: {
                                        'User-Agent': getProjectNameFromURL(issue.comments_url)
                                    },
                                    auth: githubAuth
                                };
                                request(options, function(error, response, body) {
                                    if (error) {
                                        console.log('ERROR', error);
                                        parallelCallback(error)
                                    }
                                    if (!error && response.statusCode === 200) {
                                        //  console.log('comments:', body);
                                        parallelCallback(null, body)
                                    }


                                })
                            }
                        ], function(err, result) {
                            if (err) {
                                console.log('ERROR:', err);
                                callback(err);
                            }
                            console.log('parallel process done');
                            issue.events = JSON.parse(result[0]);
                            issue.comments = JSON.parse(result[1]);
                            finalResult.push(issue)
                            callback(null, finalResult) // offending line#1
                        })
                    }) // forEach end
                }
            ], function(err, result) {
                if (err) {
                    res.send(err);
                }
                console.log('waterfall done');
                console.log(result);
                res.send(result); // offending line#2
            })

        } // if end
    }) // reqest end

}

错误

UncaughtException: Can't set headers after they are sent.
ERROR Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (http.js:691:11)
    at ServerResponse.res.set.res.header (/Users/sreddy/space/angularjs/BugTrack/node_modules/express/lib/response.js:524:10)
    at ServerResponse.res.send (/Users/sreddy/space/angularjs/BugTrack/node_modules/express/lib/response.js:125:10)
    at ServerResponse.res.json (/Users/sreddy/space/angularjs/BugTrack/node_modules/express/lib/response.js:191:15)
    at /Users/sreddy/space/angularjs/BugTrack/server/api/common/common.controller.js:163:33
    at /Users/sreddy/space/angularjs/BugTrack/server/api/common/common.controller.js:153:29
    at /Users/sreddy/space/angularjs/BugTrack/node_modules/async/lib/async.js:254:17
    at done (/Users/sreddy/space/angularjs/BugTrack/node_modules/async/lib/async.js:135:19)
    at /Users/sreddy/space/angularjs/BugTrack/node_modules/async/lib/async.js:32:16
    at /Users/sreddy/space/angularjs/BugTrack/node_modules/async/lib/async.js:251:21

最终工作代码

exports.listGitHubBugs = function(req, res) {
var _page = req.query.page || 1;
var _per_page = req.query.per_page || 25;
var finalResult = []
    //console.log('url:', 'https://api.github.com/repos/marklogic/' + req.query.project + '/issues?page=' + _page + '&per_page=' + _per_page);
var options = {
    url: 'https://api.github.com/repos/marklogic/' + req.query.project + '/issues?page=' + _page + '&per_page=' + _per_page,
    headers: {
        'User-Agent': req.query.project
    },
    auth: githubAuth
};
request(options, function(error, response, body) {
    if (error) {
        console.log(error);
        return res.send(error);
    }

    if (!error && response.statusCode === 200) {
        var issues = JSON.parse(response.body)

        async.waterfall([
            // get events and comments for all bugs and return the final processes list of bugs
            function getEventsAndCommentsForAllBugs(callback) {
                issues.forEach(function getEventsAndComments(issue, index) {
                    // for each bug, get comments and events
                    async.parallel([

                        function getEvents(parallelCallback) {
                            var options = {
                                url: issue.events_url,
                                headers: {
                                    'User-Agent': getProjectNameFromURL(issue.events_url)
                                },
                                auth: githubAuth
                            };
                            request(options, function(error, response, body) {
                                if (error) {
                                    console.log('ERROR', error);
                                    parallelCallback(error)
                                }
                                if (response.statusCode === 200) {
                                    // console.log('events:', body);
                                    parallelCallback(null, body)

                                }


                            })
                        },
                        function getComments(parallelCallback) {
                            var options = {
                                url: issue.comments_url,
                                headers: {
                                    'User-Agent': getProjectNameFromURL(issue.comments_url)
                                },
                                auth: githubAuth
                            };
                            request(options, function(error, response, body) {
                                if (error) {
                                    console.log('ERROR', error);
                                    parallelCallback(error)
                                }
                                if (response.statusCode === 200) {
                                    //  console.log('comments:', body);
                                    parallelCallback(null, body)
                                }


                            })
                        }
                    ], function attachEventsAndComments(err, result) {
                        if (err) {
                            console.log('ERROR:', err);
                            callback(err);
                        }
                        console.log('parallel process done');
                        issue.eventList = JSON.parse(result[0]);
                        issue.commentList= JSON.parse(result[1]);
                        finalResult.push(issue)
                        if (index === (issues.length - 1)) {
                            callback(null, finalResult)
                        }
                        //
                    })
                }) // forEach end

            }
        ], function processedBugs(err, result) {
            if (err) {
                res.send(err);
            }
            console.log('waterfall done');
            console.log(result);
            res.send(result);
        })

    } // if end
}) // reqest end
}

2 个答案:

答案 0 :(得分:0)

我明白了。我在每次迭代时调用parellelCallback()而不是在循环结束时调用它。如果条件完全是必需的,那就很简单。

if (index === (issues.length-1)) {
    callback(null, finalResult)
  }

答案 1 :(得分:0)

你可以提供一个完整的代码示例,我们可以尝试。

这就是说,这个源代码中有几个错误。

在第一次请求时,如果发生错误,则将其写入app.response,但不要停止执行。因此,如果发生错误,您将写入两次响应对象。

你应该这样做 if (error) { console.log(error); return res.send(error); }

而不是 if (error) { console.log(error); res.send(error); }

然后,这可以改变 if (!error && response.statusCode === 200) {if (response.statusCode === 200) {

在提取问题事件和评论时发生同样的错误,请考虑修复它。

同样在async的最后回调中.//

在async.waterfall的最终回调中。

最后,我建议您使用命名函数。这将通过提供更有意义的错误堆栈跟踪来帮助您进行调试。

例如,而不是做, async.prallel([function(){/* code here*/}]);

你会写的 async.parallel([function nameOfTheTask(){/* code here*/}]);

还考虑使用像eslint这样的linter,多个遗失;可能会破坏你的代码,请参阅http://eslint.org/