当我有2个名称相似的条目时出现错误

时间:2019-04-03 10:55:34

标签: node.js mongoose

当我有两个具有相同标题的类似帖子时,会出现此错误。 当我访问具有相同名称的页面时遇到错误,让其他具有不同名称的条目正常工作即可。

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:470:11)
    at ServerResponse.header (D:\DD\documents\100dc\nodejs\gamesplayed\node_modules\express\lib\response.js:767:10)
    at ServerResponse.send (D:\DD\documents\100dc\nodejs\gamesplayed\node_modules\express\lib\response.js:170:12)
    at done (D:\DD\documents\100dc\nodejs\gamesplayed\node_modules\express\lib\response.js:1004:10)
    at tryHandleCache (D:\DD\documents\100dc\nodejs\gamesplayed\node_modules\ejs\lib\ejs.js:257:5)
    at View.exports.renderFile [as engine] (D:\DD\documents\100dc\nodejs\gamesplayed\node_modules\ejs\lib\ejs.js:482:10)
    at View.render (D:\DD\documents\100dc\nodejs\gamesplayed\node_modules\express\lib\view.js:135:8)
    at tryRender (D:\DD\documents\100dc\nodejs\gamesplayed\node_modules\express\lib\application.js:640:10)
    at Function.render (D:\DD\documents\100dc\nodejs\gamesplayed\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (D:\DD\documents\100dc\nodejs\gamesplayed\node_modules\express\lib\response.js:1008:7)
    at games.forEach.game (D:\DD\documents\100dc\nodejs\gamesplayed\app.js:80:21)
    at Array.forEach (<anonymous>)
    at Game.find (D:\DD\documents\100dc\nodejs\gamesplayed\app.js:77:15)
    at D:\DD\documents\100dc\nodejs\gamesplayed\node_modules\mongoose\lib\model.js:4759:16
    at D:\DD\documents\100dc\nodejs\gamesplayed\node_modules\mongoose\lib\query.js:4099:12
    at process.nextTick (D:\DD\documents\100dc\nodejs\gamesplayed\node_modules\mongoose\lib\helpers\query\completeMany.js:35:39)
app.post('/newentry', (req, res)=> {
    const gameName = req.body.gameName;
    const gameImage = req.body.gameImage;
    const gamedesc = req.body.gamedesc;
    const gameEntry = new Game({title: gameName, description: gamedesc, image: gameImage});
    gameEntry.save();
    res.redirect('/');
});

app.get('/:title', (req, res) => {
    const gameTitle = _.lowerCase(req.params.title);
    Game.find((err, games)=> {
        games.forEach(game => {
            const storedGame = _.lowerCase(game.title);
            if (storedGame === gameTitle) {
                res.render('post', {title: game.title, image: game.image, desc: game.description, id: game._id })
            }
        });
    });
});

3 个答案:

答案 0 :(得分:0)

之所以发生,是因为storedGame === gameTitle在forEach循环中多次变为true。您不能在同一请求中多次致电res.render

由于看起来您正在使用猫鼬,因此建议您使用Game.find来真正找到您要查找的内容,而不是从数据库中提取所有内容并自己进行搜索。

Games.find({title: gameTitle}, (err, games) => {
  // Return what you want to the client.
})

这仍将导致退回多个游戏,因此您需要以某种方式处理该问题,然后才能响应客户端。

还要研究Promises,因为它们会使代码看起来更简洁。

答案 1 :(得分:0)

当我们尝试多次发送响应时,会发生此错误。 const storageGame = _.lowerCase(game.title)将具有多个值,因此在比较之后,它将在res.render块中存在多次,因此将发生此错误。 因此,比较应该在查询本身中完成。

答案 2 :(得分:0)

使用find时,它将返回所有游戏。因此,您可以尝试这样的操作。

    Game.find((err, games)=> {
        games= games.map(game => {
            const storedGame = _.lowerCase(game.title);
            if (storedGame === gameTitle) {
               return {title: game.title, image: game.image, desc: game.description, id: game._id }
            }
        });
     return res.send(games)
    });

另一种方法是使用ORM的findOne方法仅查找一条记录。