2012-03-05 45 views
5

Có gì sai ở đây?Lỗi ExpressJS res.render() (JSON.stringify không thể hoạt động trên tham chiếu vòng tròn)

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

Nó dẫn đến

Converting circular structure to JSON
lỗi, (kết quả trong phần tử phiên mà có một tài liệu tham khảo sử dụng hình tròn.)

 

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 || [] 
         } 
      }); 
     } 
    }); 
}; 

 

tôi đã thêm một số khai thác gỗ trong 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); 
... 

Đây là những gì tôi mong đợi trong phiên giao dịch, về cấu trúc:

 
{ 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 } } 

Nhưng đây là những gì tôi tìm thấy:

 
{ 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's 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's 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) 

Xem cách đối tượng người dùng được lồng?

  • Lưu ý rằng lần này tôi đã không gửi giá trị trong một cách rõ ràng với 'người dân địa phương' nhưng nó đã kết thúc trong một (thats nguồn gốc của tham chiếu vòng tròn.

Dường như phiên là . được sử dụng để chuyển đối tượng đến xem

Dưới đây là trung duy nhất của tôi (nó chỉ đọc từ phiên):

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

và thời gian duy nhất tôi thay đổi req.session là ở tuyến đường này:

 
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:''}) 
     } 
    }); 
}); 

Tôi không có nhiều khác đang xảy ra trong ứng dụng của tôi nêu ra, như nó vẫn còn khá trẻ. Tôi biết tôi không mangling phiên bất cứ nơi nào bản thân mình; Tôi đã kiểm tra.

Tôi đã thực hiện một số thử nghiệm khác và dường như đây chỉ là vấn đề khi tôi cố sử dụng biến cục bộ trên một trang. Ví dụ, đây là quan điểm của tôi 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") 

Nếu tôi nhận xét ra người sử dụng một phần, nó ám, nếu không tôi nhận được Converting circular structure to JSON vấn đề này.

CẬP NHẬT Vì vậy, sau khi hooking lên nhật thực và debugger v8, tôi đã bước qua đoạn code và tôi biết nơi mashup phiên và sử dụng đối tượng đang xảy ra,

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

utils .union kết thúc nghiền các thành viên của đối tượng người dùng vào phiên làm việc, gây ra tham chiếu vòng tròn. Tôi chỉ không chắc tại sao (phải thừa nhận là mã của tôi)

+0

Bạn đã gán req.session.user cho "u" trong cuộc gọi lại đó. Chỉ đơn giản là đi qua người sử dụng: u làm việc cho các cuộc gọi render, tránh tham chiếu thêm? –

+0

Không, điều đó cũng dẫn đến vấn đề tham chiếu vòng tròn. Tôi giả định đây là tất cả bởi vì tôi chỉ không hiểu một số đóng cửa cụ thể ... Tôi đào sâu vào việc thực hiện phiên bên trong kết nối - và console.log (sess) nơi nó thực hiện việc xâu chuỗi - và những gì tôi nhận được trông giống như một mashup của các biến phiên (tôi sẽ thêm vào câu hỏi cấu trúc tôi nhận được) – dwerner

+0

Nó đang được thêm vào phiên, với một tham chiếu đến chính nó đã có trong phiên ... – dwerner

Trả lời

3

Đây là vấn đề với dữ liệu phiên đang được sửa đổi trong chế độ xem.

Sau nhiều lần đào, tôi phát hiện ra rằng đó là lỗi trong cách thức chia tay được xử lý trong 2.5.8. Tôi đã gửi issue và sau đó là patch.(trong trường hợp ai đó cần thông tin này vào một ngày trong tương lai) vì npm vẫn đang cung cấp Express 2.5.8 AFAIK.

Cảm ơn sự giúp đỡ của bạn @freakish và @Ryan

+1

Làm tốt lắm. Tôi không biết tại sao tôi không thể tạo lại lỗi. quản lý để sửa lỗi này! – freakish

Các vấn đề liên quan