title: How to access a variable outside a function
body: <p>Please bear with me on this. I can't seem to get my head round how I will be able to update the variable outside the FindOne function.</p>
  
  <p>So:</p>
  
  <pre><code>var userPostCount;
  
      userPostsModel.findOne({'profileID':req.session.id}, function(err, usersPostCountDB) {
          if(usersPostCountDB){
              userPostCount = req.body.postsCount + 1;
          } else {
              console.log('There was an error getting the postsCount');
         }
      });
  </code></pre>
  
  <p>I thought it should be able to find the userPostCount variable as it's the next level up in the scope chain.</p>
  
  <p>Any ideas how I will be able to access it please? I need that variable to be outside of the function as I will be using it later for other mongoose activities.</p>
  
  <p>I am getting the userPostCount as undefined.</p>
  
  <p>Thanks in advance for the help!</p>
  
  <p>PS: I looked around for other questions on SO but the solution on other answers don't seem to work for me.</p>
  
  <p>Shayan</p>