title: Express/Mongoose Router: 'Cast to ObjectId failed for value "undefined" at path "_id"'
body: <p>I have a simple API in Express that allows the user to 'post' and 'delete' a post title into a MongoDB database. For some reason, when I add a post title, then subsequently 'delete' it, I get the "Cast to ObjectId failed for value "undefined" at path "_id". </p>
  
  <p>It seems that the "_id" doesn't exist when I call 'delete' after I create the post. However, when I refresh the page, then click 'delete', it gets the "_id" perfectly fine and deletes the entry.</p>
  
  <p>Am I doing something wrong in the routing to not have the "_id" generate and able to be pulled from the post immediately?</p>
  
  <pre><code>module.exports = function(router) {
  
      var Post = require('../models/post.js');
  
      // middleware for the api requests
      router.use(function(req, res, next) {
          // do logging
          console.log('something is happening.');
          next(); // make sure we go to our next route and don't stop here
      });
  
      // test route to make sure everything is working (accessed at GET http://localhost:8080/api)
  
      router.get('/', function(req, res) {
          res.json({ message: 'hooray! welcome to our api!' });   
      });
  
      // all routes here
  
      // routes that end in /posts
      router.route('/posts')
  
          // create a Post (accessed at POST http://localhost:7777/api/posts)
          .post(function(req, res) {
              var post = new Post();
              post.postTitle = req.body.postTitle; // set the post name (comes from request) 
              console.log(post._id);
  
              // save post and check for errors
              post.save(function(err) {
                  if (err)
                      return res.status(300).send(err);
  
                  res.json({ message: 'post created!' });
              });
          })
  
          // get all Posts (accessed at GET http://localhost:7777/api/posts)
          .get(function(req, res) {
              Post.find(function(err, posts) {
                  if (err)
                      return res.send(err);
  
                  res.json(posts);
              });
          });
  
      // routes that end in /posts for specific id
      router.route('/posts/:post_id')
  
          // get the post with that id
          .get(function(req, res) {
              Post.findById(req.params.post_id, function(err, post) {
                  if (err)
                      return res.send(err);
  
                  res.json(post);
              });
          })
  
          // update the post with that id
          .put(function(req, res) {
              Post.findById(req.params.post_id, function(err, post) {
                  if (err)
                      return res.send(err);
  
                  post.postTitle = req.body.postTitle;
  
                  // save the post
                  post.save(function(err) {
                      if (err)
                          return res.send(err);
  
                      res.json({ message: 'post updated!' });
                  });
              });
          })
  
          // deletes the post with that id
          .delete(function(req, res) {
              Post.findOne({
                  _id:req.params.post_id
              }).remove(function(x){
                  console.log("removed: ", x);
              });
          })
  
          .patch(function(req, res) {
              Post.findOne({
                  _id: req.body._id
              }, function(err, doc) {
                  for (var key in req.body) {
                      dock[key] = req.body[key];
                  }
                  doc.save();
                  res.status(200).send();
              });
          });
  }
  </code></pre>
  
  <p>/</p>
  
  <pre><code>function addPostItem(post){
          posts.push(post);
          triggerListeners();
  
          helper.post("/api/posts", post);
      }
  
  function deletePost(post) {
          var index = posts.indexOf(post);
          console.log(post);
          posts.splice(index, 1);
          triggerListeners();
  
          helper.del('api/posts/' + post._id);
      }
  </code></pre>
  
  <p>/</p>
  
  <pre><code>var $ = require('jquery');
  
  module.exports = {
      get: function(url) {
          return new Promise(function(success, error) {
              $.ajax({
                  url: url,
                  dataType: 'json',
                  success: success,
                  error: error
              });
          });
      },
      post: function(url, data) {
          return new Promise(function(success, error) {
              $.ajax({
                  url: url,
                  type: 'POST',
                  data: data,
                  success: success,
                  error: error
              });
          });
      },
      patch: function(url, data) {
          return new Promise(function(success, error) {
              $.ajax({
                  url: url,
                  type: 'PATCH',
                  data: data,
                  success: success,
                  error: error
              });
          });
      },
      del: function(url) {
          return new Promise(function(success, error) {
              $.ajax({
                  url: url,
                  type: 'DELETE',
                  success: success,
                  error: error
              });
          });
      }
  };
  </code></pre>