{"id":3283,"date":"2014-03-21T20:22:18","date_gmt":"2014-03-21T20:22:18","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/21\/nodejs-undefined-var-gets-error-collection-of-common-programming-errors-2\/"},"modified":"2014-03-21T20:22:18","modified_gmt":"2014-03-21T20:22:18","slug":"nodejs-undefined-var-gets-error-collection-of-common-programming-errors-2","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/21\/nodejs-undefined-var-gets-error-collection-of-common-programming-errors-2\/","title":{"rendered":"NodeJS undefined var gets error-Collection of common programming errors"},"content":{"rendered":"<p>I hope this is a quite easy question but I couldn&#8217;t understand why is not working.<\/p>\n<p>My problem, I access to a variable that could be not defined.<\/p>\n<p>This is the function:<\/p>\n<pre><code>  app.get('\/loc', function (req, res) {\n\n    if (typeof req.user.userId === 'undefined'){\n        redirect('\/');\n    } else {\n        var userId = req.user.userId;\n        Loc.getP([userId], function(promos) {\n           res.render('local\/index', {\n            title: 'Local'\n          });   \n        });\n    }\n  });\n<\/code><\/pre>\n<p>The problem is at the line that I&#8217;m checking if the var is undefined. If not, I just want to redirect to other url. If it&#8217;s defined, render data of the user. Quite simple, but always I try to access req.user.userId I get the following:<\/p>\n<pre><code>500 TypeError: Cannot read property 'userId' of undefined\n<\/code><\/pre>\n<p>I tried everything I found at internet, but I think in JS it has to work&#8230;<\/p>\n<p>Any idea? Thanks!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I hope this is a quite easy question but I couldn&#8217;t understand why is not working. My problem, I access to a variable that could be not defined. This is the function: app.get(&#8216;\/loc&#8217;, function (req, res) { if (typeof req.user.userId === &#8216;undefined&#8217;){ redirect(&#8216;\/&#8217;); } else { var userId = req.user.userId; Loc.getP([userId], function(promos) { res.render(&#8216;local\/index&#8217;, { [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3283","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3283","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=3283"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/3283\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=3283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=3283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=3283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}