partial views with options from session lead to JSON.stringify error (circular reference) #1039

Closed
dwerner opened this Issue Mar 8, 2012 · 6 comments

Projects

None yet

2 participants

@dwerner

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 renderPartial() in express/lib/view.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.

@tj
expressjs member
tj commented Mar 8, 2012

hmm well the view system doesn't JSON.stringify() anything so that should be unrelated

@dwerner

Sorry yeah, the stringify issue happens when the session is saved, after the partial is 'rendered'

@tj
expressjs member
tj commented Mar 8, 2012

ohhh i see what you're saying

@dwerner

If I made a copy of the partial's subject entity, would that essentially solve this issue? It strikes me that this is an issue because 'user' in this case is mutable. (Or do things work this way in 3.x? e.g. should I just upgrade?)

@dwerner

I'll close this to clean things up a bit. If anyone is interested in a fix for 2.5.8, see my repo of that branch. I'm guessing that I'm 'doing it wrong' anyhow. Tj does say explicitly that the in-memory session store is not for production.

@dwerner dwerner closed this Mar 24, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment