0

I'm new to Node. Trying to wrap my head around this error I get, I don't understand why. I seen a couple of posts with it, but they are all different cases and none kinda match mine. So appreciate help. From what I seen in other posts It's most cases in a post, mine is a get and I do return res on error.

 GET /posts/56f2a808653270ff11f7ddb2 304 30.391 ms - -
 _http_outgoing.js:344
     throw new Error('Can\'t set headers after they are sent.');
     ^

 Error: Can't set headers after they are sent.
     at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:344:11)
     at ServerResponse.header (/Users/me/nodyhead/node_modules/express/lib/response.js:718:10)
     at ServerResponse.json (/Users/me/nodyhead/node_modules/express/lib/response.js:246:10)
     at /Users/me/nodyhead/routes/index.js:50:13
     at next (/Users/me/nodyhead/node_modules/mongoose/lib/model.js:2491:5)
     at EventEmitter.<anonymous> (/Users/me/nodyhead/node_modules/mongoose/lib/model.js:2621:5)
     at EventEmitter.<anonymous> (/Users/me/nodyhead/node_modules/mpromise/lib/promise.js:180:45)
     at emitThree (events.js:97:13)
     at EventEmitter.emit (events.js:175:7)
     at Promise.safeEmit (/Users/me/nodyhead/node_modules/mpromise/lib/promise.js:83:21)
     at Promise.fulfill (/Users/me/nodyhead/node_modules/mpromise/lib/promise.js:104:24)
     at Promise.resolve (/Users/me/nodyhead/node_modules/mongoose/lib/promise.js:142:23)
     at next (/Users/me/nodyhead/node_modules/mongoose/lib/model.js:2687:15)
     at /Users/me/nodyhead/node_modules/kareem/index.js:177:19
     at /Users/me/nodyhead/node_modules/kareem/index.js:109:16
     at nextTickCallbackWith0Args (node.js:420:9) [nodemon] app crashed - waiting for file changes before starting...

This is my three functions it complains about

router.get('/posts', function(req, res, next) {
    Post.find(function(err, posts) {
        if (err) { return next(err); }

        res.json(posts);
    });
});


router.param('post', function(req, res, next, id) {
    var query = Post.findById(id);

    query.exec(function(err, post) {
        if (err) { return next(err); }
        if (!post) { return next(new Error('can\'t find post')); }

        req.post = post;
        return next();
    });
});

router.get('/posts/:post', function(req, res) {
    req.post.populate('comments', function(err, post) {
        if (err) { return next(err); }

        res.json(post);
    });

    res.json(req.post);
});
Dejan.S
  • 18,571
  • 22
  • 69
  • 112

2 Answers2

0

Well, the issue is that you call res.json() twice. See answer from this topic Error: Can't set headers after they are sent to the client for more information

Community
  • 1
  • 1
Vladimir G.
  • 885
  • 7
  • 15
0

At your GET posts/:post (your last handler) remove one of the res.json(obj) lines. The point here is that your are responding twice: one before the callback execution and one after the DB access. Keep just one of them.

Manu Artero
  • 9,238
  • 6
  • 58
  • 73