NodeJS undefined var gets error-Collection of common programming errors

I hope this is a quite easy question but I couldn’t understand why is not working.

My problem, I access to a variable that could be not defined.

This is the function:

  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'
          });   
        });
    }
  });

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:

500 TypeError: Cannot read property 'userId' of undefined

I tried everything I found at internet, but I think in JS it has to work…

Any idea? Thanks!