title: NodeJS undefined var gets error
body: <p>I hope this is a quite easy question but I couldn't understand why is not working.</p>
  
  <p>My problem, I access to a variable that could be not defined.</p>
  
  <p>This is the function:</p>
  
  <pre><code>  app.get('/loc', function (req, res) {
  
      if (typeof req.user.userId === 'undefined'){
          redirect('/');
      } else {
          var userId = req.user.userId;
          Loc.getP([userId], function(promos) {
             res.render('local/index', {
              title: 'Local'
            });   
          });
      }
    });
  </code></pre>
  
  <p>The problem is at the line that I'm checking if the var is undefined. If not, I just want to redirect to other url. If it's defined, render data of the user. Quite simple, but always I try to access req.user.userId I get the following:</p>
  
  <pre><code>500 TypeError: Cannot read property 'userId' of undefined
  </code></pre>
  
  <p>I tried everything I found at internet, but I think in JS it has to work...</p>
  
  <p>Any idea? Thanks!</p>