Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools

Michael Randall
PLUS
Michael Randall
Courses Plus Student 10,643 Points

POST method not working: no todos going to postman or mongo

My project has worked well up to this point in the Mean Stack tutorial, but I've finally hit something that doesn't work. When I use postman to post data no todos appear. Using postman doesn't cause my server to crash, however, when I try to post to the database, my server crashes, here is the error I'm getting when the server crashes when I click send to post the data in postman.

/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.<anonymous> (/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...

Here is the JSON output:

    todos: [
    {
    _id: "56db8b1b8e3135591b4f94ea",
    completed: false,
    name: "Grow pizza",
    __v: 0
    },
    {
    _id: "56db8b1b8e3135591b4f94e9",
    completed: false,
    name: "Eat cat food",
    __v: 0
    },
    {
    _id: "56db8b1b8e3135591b4f94ec",
    completed: false,
    name: "Turn on porch light",
   __v: 0
   },
   {
   _id: "56db8b1b8e3135591b4f94eb",
  completed: false,
  name: "Weed the cow",
  __v: 0
  },
  {
  _id: "56db8b1b8e3135591b4f94ed",
  completed: false,
  name: "Skim the banana",
  __v: 0
  },
  {
  _id: "56db8b1b8e3135591b4f94ee",
  completed: false,
  name: "Roast the duck",
  __v: 0
  },
  {
  _id: "56e0f0961b2987a01e8be596",
  __v: 0
  },
  {
  _id: "56e0f119c47fb9a61e80c1f6",
  name: "Cabbage",
  completed: false,
  __v: 0
  },
  {
  _id: "56e0f6a5cd88e6be1e74d6d6",
  __v: 0
  },
  {
  _id: "56e0f7dae804c1d31e33a151",
  __v: 0
  },
  {
  _id: "56e0f85cdbebe2e41eed393e",
  __v: 0
  }
  ]

Sorry couldn't get the markdown correct, but notice that there are blank entries towards the end of the list. In my web form when I use the get method, I can see empty spaces.

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

Anyhelp?

1 Answer

So, firstly, that's not valid JSON. The keys should be in double quotes.

You might be sending the MongoDB result directly instead of converting it with JSON.

I think the error is actually here: /Users/bobsgarage/WebAppProjects/mean-todo/src/api/index.js:37:9

Can you post the code in that file?

Michael Randall
Michael Randall
Courses Plus Student 10,643 Points

You were correct, I wasn't sending as JSON, but as text. I've since figured it out. Thanks

Great! Please mark my answer as 'Best Answer' if it helped. :)