Error ExpressJS res.render () (JSON.stringify cannot work with circular reference)

What is wrong here?

res.render('/somepage', {user:req.session.user})

This leads to errors

Converting circular structure to JSON
(results in a session element that has a circular link for the user.)

exports.home =  function (req, res) {
    var entityFactory = new require('../lib/entity-factory.js').EntityFactory();
    entityFactory.get_job_task_lists({
        callback : function (err, job_task_lists) {
            res.render('home.jade', {
                        locals:{
                            title: 'Logged in.', 
                            user:req.session.user, // does not work
                            job_task_lists:job_task_lists || [] 
                        }
            });
        }
    });
};


I have added some entries to node_modules/express/node_modules/connect/lib/middleware/session/memory.js

MemoryStore.prototype.set = function (sid, sess, fn) {
  var self = this;
  process.nextTick (function () {

    console.log (sess); // this is giving the output listed

    self.sessions [sid] = JSON.stringify (sess);
...

This is what I expect the session to look like in terms of structure:

{lastAccess: 1330979534026,
  cookie: 
   {path: '/',
     httpOnly: true,
     _expires: Tue, 06 Mar 2012 00:32:14 GMT,
     originalMaxAge: 14399999},
  user: // this is the object I added to the session
   {id: 1,
     username: 'admin',
     password: '8e3f8d3a98481a9073d2ab69f93ce73b',
     creation_date: Mon, 05 Mar 2012 18:08:55 GMT } } 

:

{ lastAccess: 1330979534079, // new session
  cookie: 
   { path: '/',
     httpOnly: true,
     _expires: Tue, 06 Mar 2012 00:32:14 GMT,
     originalMaxAge: 14399999 },
  user:  // but here it is again, except now it a mashup,
         // containing members it shouldn't have, like locals,
         // and, well, everything but the first 4 properties
   { id: 1,
     username: 'admin',
     password: '8e3f8d3a98481a9073d2ab69f93ce73b',
     creation_date: '2012-03-05T18:08:55.701Z',
     locals: 
      { title: 'Logged in.',
        user: [Circular], //and now it circular
        job_task_lists: [Object] },
     title: 'Logged in.',
     user: [Circular],
     job_task_lists: [ [Object], [Object], [Object], getById: [Function] ],
     attempts: [ '/home/dan/development/aqp/views/home.jade' ],
     scope: {},
     parentView: undefined,
     root: '/home/dan/development/aqp/views',
     defaultEngine: 'jade',
     settings: 
      { env: 'development',
        hints: true,
        views: '/home/dan/development/aqp/views',
        'view engine': 'jade' },
     app: 
      { stack: [Object],
        connections: 6,
        allowHalfOpen: true,
        _handle: [Object],
        _events: [Object],
        httpAllowHalfOpen: false,
        cache: [Object],
        settings: [Object],
        redirects: {},
        isCallbacks: {},
        _locals: [Object],
        dynamicViewHelpers: {},
        errorHandlers: [],
        route: '/',
        routes: [Object],
        router: [Getter],
        __usedRouter: true },
     partial: [Function],
     hint: true,
     filename: '/home/dan/development/aqp/views/home.jade',
     layout: false,
     isPartial: true } }

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)

, ?

  • , "locals" , ( .

, .

( ):

function requiresAuthentication(req, res, next){
    if (req.session.user){
        next();
    } else {
        next(new Error('Unauthorized. Please log in with a valid account.'))
    }
}

, req.session :

app.post('/home', function (req,res,next) {
    var auth = require('./lib/authentication');
    auth.authenticate_user(req.body.user, function (user) {
        if (user){
            req.session.user = user;
            console.log('authenticated');
            res.redirect(req.body.redir || '/home');
            //next();
        } else {
            console.log('not authenticated');
            res.render('logins/new.jade', {title: 'Login Failed', redir:''})
        }
    });
});

, . , ; .

, , , . , home.jade

div(data-role="page")
    div(data-role="header")
        a(href='/logout', data-icon='delete', data-ajax="false") Log out
        h1= title
        a(href='/account', data-icon='info', data-ajax="false") Account

        != partial('user', user)


    each jtl in job_task_lists
        div(id=jtl.name, class = 'draggable_item', style='border:2px solid black;')
            #{jtl.name} - #{jtl.description} 
        a(data-icon='plus') 


    div(data-role="footer")
        h3 footer

script(src="/javascripts/home.js")

, , Converting circular structure to JSON.

UPDATE eclipse v8 , , mashup ,

in node_modules/connect/lib/middleware/session/session.js

utils.union , . , (, )

+5
1

.

, , 2.5.8. issue, . ( , - ), npm Express 2.5.8 AFAIK.

@freakish @Ryan

+3

All Articles