在PUT请求发送错误后无法设置标头

时间:2017-08-05 18:28:40

标签: javascript node.js express

每次在指定路由上尝试PUT请求时,我都会收到发送后无法设置标头错误。我无法弄清楚这里的问题是什么。我也得到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 个答案:

答案 0 :(得分:1)

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

  1. 当出现错误时,您不会退出该功能。因此,您的代码会发送两次响应:第一个带有错误,第二个带有数据。
  2. 你不使用承诺。这不是一个错误,但现在它已经发布了2017年,ES7已经发布,你应该避免使用回调。
  3. 固定的,改进的代码看起来如此:

    .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);
    })