title: Mongoose throw error - how to fix this?
body: <p>In my app, I am using <code>mongoose</code> to connect with <code>mlab</code> to post the data. when I use the postman to test the post, I am getting the data updated in the data base. but the console throws this error :</p>
  
  <pre><code>events.js:141
        throw er; // Unhandled 'error' event
        ^
  
  TypeError: Cannot read property 'code' of null
      at C:\Tutorials\try\NodePractical\MEAN-Family\app\routes\api.js:42:13
      at C:\Tutorials\try\NodePractical\MEAN-Family\node_modules\mongoose\lib\model.js:3336:16
      at C:\Tutorials\try\NodePractical\MEAN-Family\node_modules\mongoose\lib\document.js:1927:18
      at nextTickCallbackWith0Args (node.js:420:9)
      at process._tickCallback (node.js:349:13)
  </code></pre>
  
  <p>I am not able to understand this error at all. How to fix this? And what is wrong with my code here?</p>
  
  <p>For the reference here is my <code>post</code> api which I am using:</p>
  
  <pre><code>.post(function( req, res ){
  
                  var family = new Family();
  
                  family.username = req.body.username,
                  family.password = req.body.password,
                  family.familyLeader = req.body.familyLeader,
                  family.husband = req.body.husband,
                  family.wife = req.body.wife,
                  family.kids = req.body.kids;
  
                  family.save(function(err) {
  
                      if (err.code == 11000) {
                          return res.json({ success: false, message: 'A user with that username already exists. '});
                      }
                      else {
                          res.send( err );
                      }
  
                      res.json({ message: 'Family created!' });
  
                  });
  
  
  
              })
  </code></pre>