Javascript 无法在已发送后发送标题?

Javascript 无法在已发送后发送标题?,javascript,node.js,Javascript,Node.js,就以下代码而言,res.send工作正常,但在我的控制台中,我收到以下消息: http.js:689 throw new Error('Can\'t set headers after they are sent.'); app.get('/summoner/:summonerName', function(req, res) { lolapi.Summoner.getByName(req.params.summonerName, function(err, obj) { var opti

就以下代码而言,res.send工作正常,但在我的控制台中,我收到以下消息:

http.js:689
throw new Error('Can\'t set headers after they are sent.');

app.get('/summoner/:summonerName', function(req, res) {
lolapi.Summoner.getByName(req.params.summonerName, function(err, obj) {
  var options = {
    beginIndex: 0,
    endIndex: 1
  };
  lolapi.MatchList.getBySummonerId(obj['savisaar2'].id, options, function(err, matches) {
    var gameMatches = matches.matches;
    for(var i = 0; i < gameMatches.length; i++) {
      lolapi.Match.get(gameMatches[i].matchId, function(err, games) {
        res.send({profile : obj, matchHistory : games});
      });
    }
  });
});
});
http.js:689 抛出新错误('发送头后无法设置头'); app.get('/courger/:courderName',函数(请求、恢复){ lolapi.courger.getByName(req.params.courderName,函数(err,obj){ 变量选项={ beginIndex:0, 完索引:1 }; lolapi.MatchList.getBySummonerId(obj['savisar2'].id,选项,函数(err,matches){ var gameMatches=matches.matches; 对于(var i=0;i正如我在评论中解释的那样,您正在
for
循环中调用
res.send()
,这意味着您多次调用它。每个请求只能调用它一次。这就是您在控制台中看到错误消息的原因

目前还不清楚您的代码真正想要做什么,但如果希望将所有结果收集到一个数组中并作为响应发送,那么您可以这样做:

app.get('/summoner/:summonerName', function (req, res) {
    lolapi.Summoner.getByName(req.params.summonerName, function (err, obj) {
        if (err) {
            return res.status(500).end();
        }
        var options = {beginIndex: 0, endIndex: 1};
        lolapi.MatchList.getBySummonerId(obj['savisaar2'].id, options, function (err, matches) {
            var gameMatches = matches.matches;
            var results = [];
            for (var i = 0; i < gameMatches.length; i++) {
                lolapi.Match.get(gameMatches[i].matchId, function (err, games) {
                    if (err) {
                        return res.status(500).end();
                    }
                    results.push({profile: obj, matchHistory: games});
                    // if all results are done, then send response
                    if (results.length === gameMatches.length) {
                        res.json(results);
                    }
                });
            }
        });
    });
});
app.get('/summoner/:summonerName', function (req, res) {
    lolapi.Summoner.getByName(req.params.summonerName, function (err, obj) {
        if (err) {
            return res.status(500).end();
        }
        var options = {beginIndex: 0, endIndex: 1};
        lolapi.MatchList.getBySummonerId(obj['savisaar2'].id, options, function (err, matches) {
            var gameMatches = matches.matches;
            var results = new Array(gameMatches.length);
            var cntr = 0;
            for (var i = 0; i < gameMatches.length; i++) {
                (function(index) {
                    lolapi.Match.get(gameMatches[i].matchId, function (err, games) {
                        if (err) {
                            return res.status(500).end();
                        }
                        ++cntr;
                        results[index] = {profile: obj, matchHistory: games};
                        // if all results are done, then send response
                        if (cntr === gameMatches.length) {
                            res.json(results);
                        }
                    });
                })(i);
            }
        });
    });
});

每个请求只能调用
res.send()
一次可能的副本。您在一个循环中多次调用它。此代码的目的是什么?您正在为
循环调用
lolapi.Match.get()
,然后尝试为每个
lolapi.Match.get()
执行
res.send()
。对给定请求多次调用
res.send()
,就是导致出现错误的原因。但是,代码应该如何工作?你应该怎么处理你所有的结果?您是否正在尝试收集所有结果,然后将它们全部作为响应发送?
const Promise = require('bluebird');
Promise.promisifyAll(lolapi.Summoner);
Promise.promisifyAll(lolapi.MatchList);
Promise.promisifyAll(lolapi.Match);

app.get('/summoner/:summonerName', function (req, res) {
    var main;
    lolapi.Summoner.getByNameAsync(req.params.summonerName).then(function(obj) {
        main = obj;
        var options = {beginIndex: 0, endIndex: 1};
        return lolapi.MatchList.getBySummonerIdAsync(obj['savisaar2'].id, options);
    }).then(function(matches) {
        var gameMatches = matches.matches;
        return Promise.map(gameMatches, function(item){
            return lolapi.Match.getAsync(item.matchId).then(function(games) {
                return {profile: main, matchHistory: games};
            });
        });
    }).then(function(results) {
        res.json(results);
    }).catch(function(err) {
        res.status(500).end();
    });
}