title: TypeError: Cannot read property '_id' of null, only in production not dev
body: <p>My development server is crashed and won't restart. I get the following message when I check my logs. Below is the error</p>
  
  <pre><code>throw er; // Unhandled 'error' event
   TypeError: Cannot read property '_id' of null
      at /app/routes/index.js:220:53
      at model.Query.&lt;anonymous&gt;(/app/node_modules/mongoose/lib/model.js:4092:16)
      at /app/node_modules/kareem/index.js:273:21
      at /app/node_modules/kareem/index.js:131:16
      at process._tickCallback (internal/process/next_tick.js:61:11)
  Emitted 'error' event at:
       at model.Query.&lt;anonymous(/app/node_modules/mongoose/lib/model.js:4094:13)
       at /app/node_modules/kareem/index.js:273:21
       at /app/node_modules/kareem/index.js:131:16
       at process._tickCallback (internal/process/next_tick.js:61:11)
  </code></pre>
  
  <p>This message only occurs in production and not development, this is leading me to confusion. Here is the relevant code from file app/routes/index.js</p>
  
  <pre><code>// USER PROFILE
  router.get("/users/:id", function(req, res) {
    User.findById(req.params.id, function(err, foundUser) {
      if(err) {
        req.flash("error", "Something went wrong.");
        res.redirect("/");
      }
  Line 220 Comment.find().where('author').equals(foundUser._id).exec(function(err, comments) {
        if(err) {
          req.flash("error", "Something went wrong.");
          res.redirect("/");
        }
          res.render("show", {user: foundUser, comments: comments});
        })
      });
    });
  </code></pre>
  
  <p>I included my schema for comments below. I tried to use this solution <a href="https://stackoverflow.com/questions/39612183/node-js-returns-undefined-from-mongodb-obejct-inner-value">node js returns undefined from mongodb obejct inner value</a> but it did not work for the situation.</p>
  
  <pre><code>var mongoose = require("mongoose");
  
  var CommentSchema = mongoose.Schema({
      text: String,
      created: 
          {type: Date, default: Date.now}, 
       author: {
          type: mongoose.Schema.Types.ObjectId,
          ref: "User"
      },
      blog: String
   });
  
   // pre-hook middleware to populate author in comment show route
  CommentSchema.pre('findOne', function(next) {
   this.populate('author');
    next();
  });
  
  module.exports = mongoose.model("Comment", CommentSchema);
  </code></pre>
  
  <p>Edits: Based on the discussion in the comments I did a bit more testing  I was able to get the error to occur in development as well.
   I console.logged the output and this is what I got. </p>
  
  <pre><code>    This is the id { _id: 5af9af45f5a3b31bd4492e9d,
    username: 'mrboston',
    firstName: 'Mr',
    lastName: 'Boston',
    email: 'fake@yahoo.com',
    avatar: 'google.com',
    __v: 0,
    isAdmin: true }
  This is the id undefined
  events.js:160
        throw er; // Unhandled 'error' event
        ^
  
  TypeError: Cannot read property '_id' of undefined
  </code></pre>
  
  <p>I was logged in as my admin user at the time. I then tested the console.log(req.params)</p>
  
  <pre><code>{ id: '5af9af45f5a3b31bd4492e9d' }
  { id: 'google.com' }
  events.js:160
        throw er; // Unhandled 'error' event
  </code></pre>
  
  <p>Does this mean I am not being specific enough of my req.params query?</p>
  
  <p>Edit 2:</p>
  
  <p>Updated the code to this:</p>
  
  <pre><code>// USER PROFILE
  router.get("/users/:id", function(req, res) {
    User.findById(req.params.id, function(err, foundUser) {
      if(err) {
        req.flash("error", "Something went wrong.");
        res.redirect("/");
      }
      if (!foundUser) {
      Comment.find().where('author').equals(foundUser._id).exec(function(err, comments) {
        if(err) {
          req.flash("error", "Something went wrong.");
          res.redirect("/");
        }
          res.render("show", {user: foundUser, comments: comments});
        });
      }
      });
    });
  </code></pre>
  
  <p>However run the app and click on the show user link, I get this error message</p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;
      &lt;meta charset='utf-8'&gt; 
      &lt;title&gt;Error 502 - Bad Gateway&lt;/title&gt;
      &lt;link rel="stylesheet" type="text/css" href="https://cdn.c9.io/errors/style.css" /&gt;
      &lt;style type="text/css"&gt;
      .error_content {
          background: rgba(255, 255, 255, 0.23);
          padding: 10px;
          width: 641px;
          margin: 25px 0;
          display: none;
      }
  
      #error-msg {
          display: block;
      }
      &lt;/style&gt;
    &lt;/head&gt;
    &lt;body class="errorUnknown light"&gt;
      &lt;div id="wrapper"&gt;
        &lt;h1&gt;Error 502 - Bad Gateway&lt;/h1&gt;
        &lt;div class="error_content" id="error-msg"&gt;
            &lt;p&gt;Please click &lt;a href="javascript:location.reload(true)"&gt;here&lt;/a&gt; to try again, if the issue persists please contact &lt;a href="https://c9.io/support"&gt;support&lt;/a&gt;&lt;/p&gt;
        &lt;/div&gt;
  
        &lt;a href="http://status.c9.io"&gt;Status Page&lt;/a&gt; |
        &lt;a href="https://c9.io/support"&gt;Support&lt;/a&gt; |
        &lt;a href="https://c9.io/dashboard.html"&gt;Dashboard&lt;/a&gt; |
        &lt;a href="https://c9.io"&gt;Home&lt;/a&gt;
      &lt;/div&gt;
    &lt;/body&gt;
  </code></pre>
  
  <p>Edit 3: UserSchema</p>
  
  <pre><code>var     mongoose          = require("mongoose");
  var passportLocalMongoose = require("passport-local-mongoose");
  
  
  var UserSchema = new mongoose.Schema({
      username: {type: String, unique: true, required: true},
      password: String,
      avatar: String,
      firstName: String,
      lastName: String,
      email: {type: String, unique: true, required: true},
      resetPasswordToken: String,
      resetPasswordExpires: Date,
      isAdmin: {type: Boolean, default: false}
  });
  
  UserSchema.plugin(passportLocalMongoose);
  
  module.exports= mongoose.model("User",UserSchema);
  </code></pre>