0

I have an Express JS app that uses express-session for session management.

I am trying to save some values to session in controller A and consume them in controller B, and send them as context to a template like so:

controllerA: function(req, res, next) {
  req.session.context = {
    id: '123123123',
    phone: '09998889999'
  };
  return res.redirect(res.locals.pathFor('verifications#phoneVerify'));
}
:
:
:
controllerB: function(req, res, next) {
  //Some code here
  return res.render('templatepath/template', req.session.context);
}

The problem is, that when I do this, I get an error at the template render step saying:

TypeError: Converting circular structure to JSON

If I instead do this as:

req.session.id: '123123123',
req.session.phone: '09998889999';

The code works just fine. Does express-session have an issue with objects?

nikjohn
  • 20,026
  • 14
  • 50
  • 86
  • theres console.log or something to log on your code? – Sebastián Espinosa Aug 30 '16 at 21:28
  • 1
    I think that problem in another place: `req.session.context` doesn't content reference by itself e.g. `req.session.context = { ..., session: req.session}`. Try to call `res.render('templatepath/template', {id: ...});` – Aikon Mogwai Aug 30 '16 at 23:00
  • @Aikon Mogwai: > `req.session.context doesn't content reference by itself e.g. req.session.context = { ..., session: req.session}` Why would there be a `session: req.session` within the `req.session.context`? I'm not sure I follow you – nikjohn Aug 31 '16 at 04:29
  • 1
    For example: some objects in `req` have self-reference (you can see them if `console.log(req)` as `[Circular]`). In your case I think that you don't have circular by `req.session.context` (but you must check this like I wrote above). Perhaps you include template to itself. If you use JSON.stringify in code then search problem there. – Aikon Mogwai Aug 31 '16 at 11:02

0 Answers0