title: Express.js blogging application bug: filtering posts by category throws "Cast to ObjectId failed" failed error
body: <p>I am working on a <strong><a href="https://github.com/Ajax30/XPressBlog" rel="nofollow noreferrer">blogging application</a></strong> (click the link to see the <strong>GitHub</strong> repo) with <a href="https://expressjs.com/" rel="nofollow noreferrer">Express</a>, <a href="https://ejs.co/" rel="nofollow noreferrer">EJS</a> and MongoDB.</p>
  
  <p>I have <em>Posts</em> that are grouped into <em>Categories</em>, each in its own collection.</p>
  
  <p>I run into a problem trying to filter posts by category. To obtain the post by category url, I turn the category name into a slug and use it this way:</p>
  
  <pre><code>Posted in &lt;a href="/&lt;%= post.category.cat_name.replace(/\s+/g, '-').toLowerCase(); %&gt;"&gt;&lt;%= post.category.cat_name %&gt;&lt;/a&gt;
  </code></pre>
  
  <p>In the public routes file I have:</p>
  
  <pre><code>const express = require('express');
  const postsController = require('../../controllers/front-end/posts');
  
  // Express router
  const router = express.Router();
  
  // Get Posts
  router.get('/', postsController.getPosts);
  
  // Get Single Post
  router.get('/:id', postsController.getSinglePost);
  
  // Get Posts by Category
  router.get('/:catname', postsController.getPostsByCategory);
  
  module.exports = router;
  </code></pre>
  
  <p>The <strong>Post</strong> model:</p>
  
  <pre><code>const mongoose = require('mongoose');
  
  const postSchema = new mongoose.Schema({
      title: {
          type: String,
          required: true
      },
      short_description: {
          type: String,
          required: true
      },
      full_text: {
          type: String,
          required: true
      },
      category: {
          type: mongoose.Schema.Types.ObjectId,
          ref: 'Category'
      },
      post_image: {
          type: String,
          required: false
      },
      updated_at: {
          type: Date,
          default: Date.now()
      },
      created_at: {
          type: Date,
          default: Date.now()
      }
  });
  
  module.exports = mongoose.model('Post', postSchema);
  </code></pre>
  
  <p>The <strong>Category</strong> model:</p>
  
  <pre><code>const mongoose = require('mongoose');
  
  const categorySchema = new mongoose.Schema({
      cat_name: {
          type: String,
          required: true
      },
      updated_at: {
          type: Date,
          default: Date.now()
      },
      created_at: {
          type: Date,
          default: Date.now()
      }
  });
  
  module.exports = mongoose.model('Category', categorySchema);
  </code></pre>
  
  <p>In the Posts controller I turn the slug back into category name to filter posts <em>by category name</em>:</p>
  
  <pre><code>exports.getPostsByCategory = (req, res, next) =&gt; {
  
      function titleize(slug) {
              var words = slug.split("-");
              return words.map(function(word) {
                  //return word;
                  return word.charAt(0).toUpperCase() + word.substring(1).toLowerCase();
              }).join(' ');
          }
  
      const postCategory = titleize(req.params.catname);
  
     const posts = Post.find({ cat_name: postCategory }, (err, posts) =&gt; {
          console.log('Category: ', postCategory);
          if(err){
              console.log('Error: ', err);
          } else {
              res.render('default/index', {
                  moment: moment,
                  layout: 'default/layout',
                  website_name: 'MEAN Blog',
                  page_heading: 'XPress News',
                  page_subheading: 'A MEAN Stack Blogging Application',
                  posts: posts.reverse(),
              });
          }
      }).populate('category');
  };
  </code></pre>
  
  <p>The line <code>console.log('Category: ', postCategory)</code> outputs <code>Category:  Favicon.ico</code> <em>instead of the category name</em>.</p>
  
  <p>What am I doing wrong?</p>