title: Node.js and mongoose (mongodb) error cannot read property '' of null
body: <p>I have a findOne query, and when ever i verify if it returned a empty document i get thrown a error saying 'cannot read property 'username' of null'. That happend when i try to acess doc.username in if(!doc.username) {</p>
  
  <p>My code:</p>
  
  <pre><code>function checkAccDb(username, password) { console.log(2);
      /* Check if accounts exists in db */
      db.findOne({username: username}, function(err, doc){ console.log(3);
          if(err) throw err;
  
          if(!doc.username) {
              add2stack(username, password);
          }
          else if(doc.status == 200) {
              end(username, password, 1000);
          }
          else if(doc.status == 401) {
              if(doc.password == password)
                  end(username, password, 401);
              else
                  add2stack(username, password);
          }
          else {
              add2stack(username, password);
          }
      });
  }
  </code></pre>
  
  <p>Could anyone please explain me what's happening here?</p>
  
  <p>Thanks!</p>