书架/护照:发送后无法设置标题

书架/护照:发送后无法设置标题

问题描述:

我遇到了路由器控制台中的一些错误。快递路线是这样的:只有当我不认证出现书架/护照:发送后无法设置标题

router.get('/getInfo/:id', function(req, res, next) { 
    return passport.authenticate('jwt', {session: false}, function(err, account, info) { 
    if (err) { return next(err);} 
    if (!account) { 
    new Account({_id: req.params.id}).fetch({columns: ['first_name']}).then(function(acc){ 
     return res.status(200).send(acc.toJSON()); 
    }); 
    } 
    if(account._id != req.params.id) { 
    new Account({_id: req.params.id}).fetch({columns: ['first_name']}).then(function(acc){ 
     return res.status(200).send(acc.toJSON()); 
    }); 
    } 
    else { 
    new Account({_id: req.params.id}).fetch().then(function(acc){ 
     return res.status(200).send(acc.toJSON()); 
    }); 
    } 
    })(req, res, next); 
}); 

错误信息 - 它说

Unhandled rejection Error: Can't set headers after they are sent. 
... 
at null.<anonymous> (/-----/routes/users.js:61:27) 

61号线是res.status(200)。在与块( account._id!= req.params.id)。

代码背后的想法是,如果你请求的信息关于不是你的用户(通过jwt验证),你只能得到一些信息 - 现在只有'first_name'。如果你要求提供关于你自己的信息,你会看到所有信息

最让我感到困惑的是我每次打电话时都会收到一个有效的回复。服务器不会崩溃/拒绝加载/ .. - 我得到了我期望的。

+0

你可能想要做'返回新帐户(...' – tbking

我认为它来自一个使用不当的承诺

的事情是,当你写这个

if (!account) { 
    new Account({_id: req.params.id}).fetch({columns: ['first_name']}).then(function(acc){ 
     return res.status(200).send(acc.toJSON()); 
    }); 
    } 

你实际上不会从函数返回,而是创建一个新帐户,并获得承诺执行res.status(200)...,但你然后继续到下一行if ... else ...你在哪里保证也得到另一个承诺res.status(200)...

所以,你确实试图设置头两次因此错误。

为了解决这个问题,你有几种解决方案

(1)你添加一个'return`在IFS所有线路

router.get('/getInfo/:id', function(req, res, next) { 
    return passport.authenticate('jwt', {session: false}, function(err, account, info) { 
    if (err) { return next(err);} 
    if (!account) { 
     return new Account({_id: req.params.id}).fetch({columns: ['first_name']}).then(function(acc){ 
     return res.status(200).send(acc.toJSON()); 
     }); 
    } 
    if(account._id != req.params.id) { 
     return new Account({_id: req.params.id}).fetch({columns: ['first_name']}).then(function(acc){ 
     return res.status(200).send(acc.toJSON()); 
     }); 
    } 
    else { 
     return new Account({_id: req.params.id}).fetch().then(function(acc){ 
     return res.status(200).send(acc.toJSON()); 
     }); 
    } 
    })(req, res, next); 
}); 

(2)您链IFS,让你相信你只有

router.get('/getInfo/:id', function(req, res, next) { 
    return passport.authenticate('jwt', {session: false}, function(err, account, info) { 
    if (err) { return next(err);} 
    if (!account) { 
     new Account({_id: req.params.id}).fetch({columns: ['first_name']}).then(function(acc){ 
     return res.status(200).send(acc.toJSON()); 
     }); 
    } else if(account._id != req.params.id) { 
     new Account({_id: req.params.id}).fetch({columns: ['first_name']}).then(function(acc){ 
     return res.status(200).send(acc.toJSON()); 
     }); 
    } else { 
     new Account({_id: req.params.id}).fetch().then(function(acc){ 
     return res.status(200).send(acc.toJSON()); 
     }); 
    } 
    })(req, res, next); 
}); 

(3),或者你调整你的代码有点多一点与承诺的意图行执行每次调用只有一个。其实你有3种不同的方式来创建一个帐户,但只有一种方式来结束通话。我会更明确地说明。这会给类似

router.get('/getInfo/:id', function(req, res, next) { 
    return passport.authenticate('jwt', {session: false}, function(err, account, info) { 
    if (err) { return next(err);} 
    let createAccountPromise; 
    if (!account) { 
     createAccountPromise = new Account({_id: req.params.id}).fetch({columns: ['first_name']}); 
    } else if(account._id != req.params.id) { 
     createAccountPromise = new Account({_id: req.params.id}).fetch({columns: ['first_name']}); 
    } else { 
     createAccountPromise = new Account({_id: req.params.id}).fetch(); 
    } 

    return createAccountPromise 
     .then(function(acc){ 
     return res.status(200).send(acc.toJSON()); 
     }); 
    })(req, res, next); 
}); 
+0

谢谢,这工作完美!我决定使用(3),并看看承诺。 – user3790241