gpt4 book ai didi

javascript - PUT 请求发送错误后无法设置 header

转载 作者:行者123 更新时间:2023-12-03 03:43:14 26 4
gpt4 key购买 nike

每次我在指定路由上尝试 PUT 请求时,都会收到发送后无法设置 header 错误。我不知道这里出了什么问题。我也收到 200 响应,但数据库中的数据未更新。

这是我的代码:

episodeRouter.route('/:episodeId/comments/:commentId')
.get(Verify.verifyOrdinaryUser, function (req, res, next) {
Episode.findById(req.params.episodeId)
.populate('comments.postedBy')
.exec(function (err, episode) {
if (err) next(err);
res.json(episode.comments.id(req.params.commentId));
});
})

.put(Verify.verifyOrdinaryUser, function (req, res, next) {
// We delete the existing commment and insert the updated
// comment as a new comment
Episode.findById(req.params.episodeId, function (err, episode) {
if (err) next(err);
episode.comments.id(req.params.commentId).remove();

req.body.postedBy = req.decoded._id;

episode.comments.push(req.body);
episode.save(function (err, episode) {
if (err) next(err);
res.json(episode);
});
});
})

.delete(Verify.verifyOrdinaryUser, function(req, res, next){
Episode.findById(req.params.episodeId, function (err, episode) {
if (err) next(err);
episode.comments.id(req.params.commentId).remove();
episode.save(function(err, resp) {
if (err) next(err);
res.json(resp);
});
});
});

这是我得到的错误:

Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)
at ServerResponse.header (H:\COURSERA\FULL STACK WEB DEVELOPMENT COURSE\CAPSTONE PROJECT\Capstone Project\watchours\node_modules\express\lib\response.js:719:10)
at ServerResponse.send (H:\COURSERA\FULL STACK WEB DEVELOPMENT COURSE\CAPSTONE PROJECT\Capstone Project\watchours\node_modules\express\lib\response.js:164:12)
at done (H:\COURSERA\FULL STACK WEB DEVELOPMENT COURSE\CAPSTONE PROJECT\Capstone Project\watchours\node_modules\express\lib\response.js:956:10)
at Object.exports.renderFile (H:\COURSERA\FULL STACK WEB DEVELOPMENT COURSE\CAPSTONE PROJECT\Capstone Project\watchours\node_modules\jade\lib\index.js:374:12)
at View.exports.__express [as engine] (H:\COURSERA\FULL STACK WEB DEVELOPMENT COURSE\CAPSTONE PROJECT\Capstone Project\watchours\node_modules\jade\lib\index.js:417:11)
at View.render (H:\COURSERA\FULL STACK WEB DEVELOPMENT COURSE\CAPSTONE PROJECT\Capstone Project\watchours\node_modules\express\lib\view.js:126:8)
at tryRender (H:\COURSERA\FULL STACK WEB DEVELOPMENT COURSE\CAPSTONE PROJECT\Capstone Project\watchours\node_modules\express\lib\application.js:639:10)
at EventEmitter.render (H:\COURSERA\FULL STACK WEB DEVELOPMENT COURSE\CAPSTONE PROJECT\Capstone Project\watchours\node_modules\express\lib\application.js:591:3)
at ServerResponse.render (H:\COURSERA\FULL STACK WEB DEVELOPMENT COURSE\CAPSTONE PROJECT\Capstone Project\watchours\node_modules\express\lib\response.js:960:7)
at H:\COURSERA\FULL STACK WEB DEVELOPMENT COURSE\CAPSTONE PROJECT\Capstone Project\watchours\app.js:91:7
at Layer.handle_error (H:\COURSERA\FULL STACK WEB DEVELOPMENT COURSE\CAPSTONE PROJECT\Capstone Project\watchours\node_modules\express\lib\router\layer.js:71:5)
at trim_prefix (H:\COURSERA\FULL STACK WEB DEVELOPMENT COURSE\CAPSTONE PROJECT\Capstone Project\watchours\node_modules\express\lib\router\index.js:310:13)
at H:\COURSERA\FULL STACK WEB DEVELOPMENT COURSE\CAPSTONE PROJECT\Capstone Project\watchours\node_modules\express\lib\router\index.js:280:7
at Function.process_params (H:\COURSERA\FULL STACK WEB DEVELOPMENT COURSE\CAPSTONE PROJECT\Capstone Project\watchours\node_modules\express\lib\router\index.js:330:12)
at next (H:\COURSERA\FULL STACK WEB DEVELOPMENT COURSE\CAPSTONE PROJECT\Capstone Project\watchours\node_modules\express\lib\router\index.js:271:10)

最佳答案

您的代码有一些错误/问题:

  1. 当出现错误时,您不会退出该函数。因此,您的代码会发送两次响应:第一次发送错误,第二次发送数据。
  2. 您不使用 Promise。这不是一个错误,但现在已经是 2017 年了,ES7 已经发布了,你应该避免使用回调。

修复、改进的代码可能看起来像这样:

.put(Verify.verifyOrdinaryUser, (req, res, next) => {
Episode
.findById(req.params.episodeId, (err, episode) => {
req.body.postedBy = req.decoded._id;

episode.comments.id(req.params.commentId).remove();
episode.comments.push(req.body);
return episode.save();
})
.then(episode => res.send(episode))
.catch(next);
})

关于javascript - PUT 请求发送错误后无法设置 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45525183/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com