node.js - Always have undefined when trying to access array in MongoDB result obj -


good time of day! need simple login/authorization personal site running on express.js. login handled password.js , works well, strange happens inside of auth middleware cannot access user's roles stored as array in mongo - it's undefined :

app.get('/admin',           isloggedin, //handled password.js - works ok,           hasrole('admin'), //problem here          function(req, res){             res.render('admin.handlebars', { layout : 'main.handlebars', user: req.user });          } );  function hasrole(role){     return function(req, res, next){         console.log(req.user);         /*output:         {            "_id" : objectid("55a8506c3e1cc054c80eb345"),            "name" : "user1",            "passhash" : "$2a$10$e.c1bniptxrtmk8nyieoqo0/0xkru2pkmaytldusvnfngc7nmb7e2",            "roles" : [                         "admin", **strong text**                        "power-user"                      ]         }*/         console.log(req.user.roles);         //output : undefined...     } } 

array presented in console.log(req.user) output undefined if try fetch directly. i'm using workaround - store roles comma-separated string, me make work array?

try this:

function hasrole(role){     return function(req, res, next){         var user = json.parse(json.stringify(req.user));         console.log(user.roles);     } } 

Comments