node.js - Express.js Routing error: Can't set headers after they are sent -


i'm not sure why i'm getting error. it's simple api built on express.js able add , remove posts. error occurs when trigger delete router. i've read error typically happens when there 2 callbacks, however, don't seem able find double callbacks.

    _http_outgoing.js:344     throw new error('can\'t set headers after sent.');     error: can't set headers after sent.     @ serverresponse.outgoingmessage.setheader (_http_outgoing.js:344:11)     @ serverresponse.header (/users/bounty/projects/_learning/react-express/node_modules/express/lib/response.js:718:10) @ serverresponse.send (/users/bounty/projects/_learning/react-express/node_modules/express/lib/response.js:163:12)     @ serverresponse.json (/users/bounty/projects/_learning/react-express/node_modules/express/lib/response.js:249:15)     @ /users/bounty/projects/_learning/react-express/server/routes/posts.js:86:9     @ nexttickcallbackwith0args (node.js:452:9)     @ process._tickcallback (node.js:381:13) 

here posts.js router:

module.exports = function(router) {      var post = require('../models/post.js');      // middleware api requests     router.use(function(req, res, next) {         // logging         console.log('something happening.');         next(); // make sure go our next route , don't stop here     });      // test route make sure working (accessed @ http://localhost:8080/api)      router.get('/', function(req, res) {         res.json({ message: 'hooray! welcome our api!' });        });      // routes here      // routes end in /posts     router.route('/posts')          // create post (accessed @ post http://localhost:7777/api/posts)         .post(function(req, res) {             var post = new post();             post.posttitle = req.body.posttitle; // set post name (comes request)               // save post , check errors             post.save(function(err) {                 if (err)                     res.send();                  res.json({ message: 'post created!' });             });         })          // posts (accessed @ http://localhost:7777/api/posts)         .get(function(req, res) {             post.find(function(err, posts) {                 if (err)                     res.send();                  res.json(posts);             });         });      // routes end in /posts specific id     router.route('/posts/:post_id')          // post id         .get(function(req, res) {             post.findbyid(req.params.post_id, function(err, post) {                 if (err)                     res.send(err);                  res.json(post);             });         })          // update post id         .put(function(req, res) {             post.findbyid(req.params.post_id, function(err, post) {                 if (err)                     res.send(err);                  post.posttitle = req.body.posttitle;                  // save post                 post.save(function(err) {                     if (err)                         res.send(err);                      res.json({ message: 'post updated!' });                 });             });         })          // deletes post id         .delete(function(req, res) {             post.remove({                 _id: req.params.post_id             }, function(err, post) {                 if (err) {                     res.send(err);                 }                 res.json({ message: 'post deleted!' });             });         }); } 

you need add 'return' don't reply twice.

// save post , check errors post.save(function(err) {     if (err) {         return res.send();     }     res.json({ message: 'post created!' }); }); 

Comments