title: NodeJS crash: Mongoose findById() does NOT throw error when document is NOT found
body: <p>I tried researching for the cause of this problem; it is most likely a misunderstanding on my part of the way Mongoose works. I was experimenting with creating a CRUD backend in REST style. GET, PUT, POST all seem to work. However, I'm a bit at a loss with a problem with the DELETE verb. </p>
  
  <h2>The problem</h2>
  
  <p>I entered some fake data into mongo with Postman and all went well. So I copied the _id field of one my records and tried to DELETE. When I sent the DELETE request, the success message came through and the record was indeed removed from mongo. However, after deleting other records, I decided to try deleting a record with an _id of a document I had already deleted.</p>
  
  <p>I sent a DELETE request to 'api/brothers/57280602953cff031f21ad9c'</p>
  
  <p>Turns out NodeJS crashes! </p>
  
  <p>Here's the relevant server code:</p>
  
  <pre><code>// DELETE a brother by id
  app.delete('/api/brothers/:id', function(req, res) {
  
      Brother.findById(req.params.id, function(err, brother) {
  
        if (err) {
          // error finding brother
          res.json(err);
  
        } else {
  
          // remove record
          brother.remove(function(err, brother) {
  
            if (err) {
              // error removing!
              res.send(err);
            } else {
  
              // successful DELETE!
              res.json({
                message : "successfully deleted",
                brother : brother
              });
            }
  
          });
        }
  
      });
  
  
    });
  </code></pre>
  
  <p>Nodejs crashed and my console window returned the following:</p>
  
  <pre><code>TypeError: Cannot read property 'remove' of null
  [nodemon] app crashed - waiting for file changes before starting...
  </code></pre>
  
  <h2>Why wasn't an error thrown by Mongoose?</h2>
  
  <p>To my understanding, an error was not thrown because, had it been thrown, my <code>if</code> statement would have executed and returned the error to the user. If I enter a bogus id in the url then I get back an error, but if I paste in an _id of a record I already deleted, an error is NOT thrown, and execution moves on to the <code>else</code> block, and goes ahead removing a document that doesn't exist. Not even the <code>.remove()</code> seems to catch the error... the server just crashes. </p>
  
  <p>So... my question is why? Does it have to do with Mongo casting the <code>_id</code>? Why isn't an error thrown? Why does the server crash.</p>
  
  <h2>Sort of a solution...</h2>
  
  <p>If I modify the <code>if</code> statement to not only catch for an error, but to also check for the existence of a <code>brother</code> returned from the <code>findById()</code> function, the error is returned to the user and the disaster is averted:</p>
  
  <pre><code>if (err || !brother) {
    // THIS WORKS
    res.json(brother);
  
   } else { ...
  </code></pre>
  
  <p>Again... why? Why if the <code>brother</code> obj is null does Mongoose even attempt to delete the document? And why does the server crash?</p>
  
  <p>// I am using Express 4.13.4 and Mongoose 4.4.12</p>
  
  <p>Thank you for your help, and pardon any novice ignorance. </p>