Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Building a MEAN Application Creating and Editing Data in a MEAN App Creating Data with POST Routes in Express

jessica grinberg
jessica grinberg
14,117 Points

post method isn't working: no todos are posting

Hi, I am following the video and everything worked well so far. Now I added the post method in index.js

router.post('/todos', function (req, res) {
  var todo = req.body;
  res.send(todo);
});

so this is my index.js

'use strict';

var express = require('express');
var Todo = require('../models/todo');
//var todos = require('../../mock/todos.json')

var router = express.Router();

router.get('/todos', function(req, res) {
  Todo.find({}, function(err, todos){
    if(err) {
      //do something
      return res.status(500).json({message: err.message});
    }
    res.json({todos: todos});
  });
});

router.post('/todos', function (req, res) {
  var todo = req.body;
  res.send(todo);
});



//TODO: Add PUT route to update existing entries

//TODO: Add DELETE route to delete entries

module.exports = router;

and my app.js

'use strict';

var express = require('express');
var parser = require('body-parser');
var router = require('./api');

var app = express();

require('./database');
require('./seed');

app.use('/', express.static('public'));
app.use(parser.json());

app.use('/api', router);


app.listen(3000, function() {
  console.log("The serving is running on port 3000!")
});

No todos are posting its just an empty json

I installed body-parser in app.js , it doesn't work but i have no error, neither in the console, nor in postman or in the terminal.

Thanks for your help!

Michael Randall
Michael Randall
Courses Plus Student 10,643 Points

I am experiencing the same problem. '''You could check to see if your server crashes each time you try to post'''. My code worked up until the point that we used postman to test that data was getting posted. The data didn't show up in postman, so I figured I would still follow along and maybe it would post to the mongo database, but it didn't. Each time I tried to post, it caused my server to crash with the following error:

/Users/bobsgarage/WebAppProjects/mean-todo/node_modules/mongodb/lib/utils.js:98 process.nextTick(function() { throw err; }); ^

Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (http_outgoing.js:346:11) at ServerResponse.header (/Users/bobsgarage/WebAppProjects/mean-todo/node_modules/express/lib/response.js:718:10) at ServerResponse.send (/Users/bobsgarage/WebAppProjects/mean-todo/node_modules/express/lib/response.js:163:12) at ServerResponse.json (/Users/bobsgarage/WebAppProjects/mean-todo/node_modules/express/lib/response.js:249:15) at Function. (/Users/bobsgarage/WebAppProjects/mean-todo/src/api/index.js:37:9) at /Users/bobsgarage/WebAppProjects/mean-todo/node_modules/mongoose/lib/model.js:1790:14 at /Users/bobsgarage/WebAppProjects/mean-todo/node_modules/async/lib/async.js:726:13 at /Users/bobsgarage/WebAppProjects/mean-todo/node_modules/async/lib/async.js:52:16 at done (/Users/bobsgarage/WebAppProjects/mean-todo/node_modules/async/lib/async.js:246:17) at /Users/bobsgarage/WebAppProjects/mean-todo/node_modules/async/lib/async.js:44:16 at /Users/bobsgarage/WebAppProjects/mean-todo/node_modules/async/lib/async.js:723:17 at /Users/bobsgarage/WebAppProjects/mean-todo/node_modules/async/lib/async.js:167:37 at model.callbackWrapper (/Users/bobsgarage/WebAppProjects/mean-todo/node_modules/mongoose/lib/model.js:1763:11) at next (/Users/bobsgarage/WebAppProjects/mean-todo/node_modules/hooks-fixed/hooks.js:89:34) at fnWrapper (/Users/bobsgarage/WebAppProjects/mean-todo/node_modules/hooks-fixed/hooks.js:186:18) at /Users/bobsgarage/WebAppProjects/mean-todo/node_modules/mongoose/lib/model.js:227:5 [nodemon] app crashed - waiting for file changes before starting...

It looks like something with the mongdb lib utils.js file, but not sure if that is the real error.

3 Answers

jessica grinberg
jessica grinberg
14,117 Points

Hey Michael, I found out my error was coming from Postman settings! when you post Json make sure you set your settings to JSON and not text!

Michael Randall
Michael Randall
Courses Plus Student 10,643 Points

Thanks Jessica! That worked with postman and mongoDB. Thanks! Unfortunately my server still crashes when I post the data to mongoDB. A discussion for another thread I guess.

Chad Danna
Chad Danna
11,541 Points

Michael Randall Did you every figure out your issue? I am experiencing the same complication as you mentioned above where my server crashes after I send a post. Thanks

Also, make sure you change the HTTP method from the default 'GET' to 'POST' in the dropdown menu immediately to the left of the URL field in Postman. That, and changing the formant from simple text to "JSON (application/json)" should resolve the problem.

Yash Gopal
Yash Gopal
4,638 Points

Make sure you set the body type to 'JSON' rather than the default 'Text'. It will be next to the radio buttons where he selects 'Raw' body.