当与Passport.js,Express和Mongoose一起使用时,我在NodeJS中遇到这个奇怪的问题。基本上,即使我发送的标题不止一个,我也会收到一条错误消息:“在将标题发送到客户端后无法设置标题”。
我读过其他文章,也尝试过,但没有一个起作用。
我已经解决了github问题,但似乎找不到解决方案。我遇到这样的问题,当我发送多个响应头时会触发此错误,但事实是我没有发送多个头。好像很奇怪
这是我的堆栈跟踪:
(节点:9236)DeprecationWarning:不建议使用当前的URL字符串解析器,并将在以后的版本中将其删除。要使用新的解析器,请将选项{useNewUrlParser:true}传递给MongoClient.connect。
服务器运行在端口5000
MongoDB的关联错误
[ERR_HTTP_HEADERS_SENT]:无法设置头后,他们被发送到客户端
在validateHeader(_http_outgoing.js:503:11)
在ServerResponse.setHeader(_http_outgoing.js:510:3)
在ServerResponse.header (/Users/lourdesroashan/code/github/devlog/node_modules/express/lib/response.js:767:10)
在ServerResponse.json(/ Users / lourdesroashan / code / github / devlog / node_modules / express / lib / response。 js:264:10
)
位于<anonymous> 上的Profile.findOne.then.profile(/Users/lourdesroashan/code/github/devlog/routes/api/profile.js:27:30)
这是我的服务器代码:
router.get("/userprofile", passport.authenticate('jwt', { session: false }), (req, res) => {
Profile.findOne({ user: req.user.id }).then(profile => {
if (!profile) {
return res.status(404).json({ error: "No Profile Found" });
}
else {
res.json(profile);
}
}).catch(err => {
console.log(err);
})
});
Run Code Online (Sandbox Code Playgroud)
我知道错误的含义,但是据我所知,我认为我没有发送多个标头,甚至通过console.log检查是否仅运行了一个块。
提前非常感谢您!:)
完整代码位于:https …