Norman Bird Norman Bird - 1 year ago 87
Jade Question

Cant figure out how to get session variable data passed to the routes res.sender function

So basically I am trying to pass a variables to the route so that as the page loads it has the updates from the session values. But I can't figure the proper way. I have tried a few ways and this latest way gets an error on the res.sender line, where the variable info is causing the error "typeError: Object is not a function".

What would the proper syntax look like if you wanted to get those req.session variables into the res.sender so the route page can have access to its content? I'm new to node and express and I can't seem to figure out the right way.

router.get('/add-rounds', function (req, res) {

// create needed game session variables
req.session.player =;

if (!req.session.round) {
req.session.round = 1;
req.session.roundTotal = 0;
req.session.achievedMastery = false;
console.log('initialized session variables');

// console.log(res.locals.round);

info = req.session;

res.render('add-rounds.jade', {csrfToken: req.csrfToken() }, info);

Answer Source

Read up on the express render method

Unless your expecting to execute a callback on your render you should send the info variable as part of variable for the view(in the second argument), as shown

res.render('add-rounds.jade', {csrfToken: req.csrfToken(), info: req.session});

I'd like to point out that sending the FULL session down to the view is bad practice. App.locals might be a better place to put and access this information.

Also, you should't have to reference the file extension in the render method. Simply 'add-rounds' should suffice, so long as the view is at the top of your views directory. I don't know what your project looks like though.

Also, I'd place your updateRound function above the render method.

To access your newly passed session variable in your jade view simply use the info key to access it. For example,

p= info.achievedMastery

Typically, if your view object begins to accumulate variables its nicer to build it outside of the argument. For example,

var data = {
  acheivedMaster: req.session.achievedMastery,
  round: req.session.round,
  roundTotal: req.session.roundTotal
  //Now lets render the view and pass the data
  return res.render('add-rounds', data);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download