visionmedia/express — Issue #1039: partial views with options from session lead to JSON.stringify error (circular reference)
Author: dwerner
Created: 2012-03-08 20:44:29+00:00
State: open

Some steps to reproduce (in Express v2.5.8)

1) Create a new project with session support (what I did)
2) Add a route that saves something to req.session (Memory)
3) pass that value to res.render () to a view that contains a partial for the type saved in the session. In my example:

some code:

app.js

app.get('/', function (req, res) {
req.session.user = {username:'dan', password:'ASDF'};
res.render('index', { title: 'Express' });
});

app.get('/test', function (req, res) {
res.render("test.jade", {title:'test', user:req.session.user});
});

test.jade

p User details:
!= partial('user', user)

_user.partial

p User Partial : #{user.username}, - #{user.password}

Initially, I got the following when trying to render the view, test.jade:

node.js:201
throw e; // process.nextTick error, or 'error' event on first tick
^
TypeError: Converting circular structure to JSON
at Object.stringify (native)
at Array.0 (/home/dan/development/aqp/node_modules/express/node_modules/connect/lib/middleware/session /memory.js:77:31)
at EventEmitter._tickCallback (node.js:192:40)

after some digging, I found that I narrowed this down to

response.renderPartial() in express/lib/response.js.

The problem, as I understand it, is that because parentLocals (in this case the session?) appears to be utils.union()'ed with the partial's object, user, causing a circular reference.

I am willing to admit that I am pretty new to node.js in general, so I could really just be using the tools wrong.