2016-04-11 28 views
0
ekspres

ile nodejs çalışmıyor ama bu verileri güncelleştirmeye çalıştığınızda, banaGüncelleme çerez ben girişten sonra bir oturum açma çerez oluşturduk

Can't set headers after they are sent. 

Benim kod aşağıda bir hata verir.

/* Logout to main user. */ /* Not Working */ 
router.get('/logoutToMain', mustBe.authorized('supportuser'), function (req, res) { 
    var loginSealed = req.cookies[req.ApplicationCookies.LoginCookie]; 
    if (loginSealed != undefined) { 
     iron.unseal(loginSealed, req.Security.CookiePassword, iron.defaults, function (err, unsealed) { 
      if (!err) { 
       unsealed.SupportUser = null; 
       iron.seal(unsealed, req.Security.CookiePassword, iron.defaults, function (err, sealed) { 
        if (!err) { 
         res.cookie(req.ApplicationCookies.LoginCookie, sealed, { expires: new Date(Date.now() + 432000), httpOnly: true, ephemeral: true }); 
        } 
       }); 
       //res.cookie(req.ApplicationCookies.LoginCookie, unsealed, { expires: new Date(Date.now() - 1000), httpOnly: true }); 
      } 
     }); 
    } 
    res.redirect('/'); 
}); 

Bu çerezi kaldırdığımda, bu işe yarar. (Kod aşağıda) kod parçasının altında

/* Logout action. */ /* Working */ 
router.get('/logout', mustBe.authenticated(), function (req, res) { 
    var LoginCookie = req.cookies[req.ApplicationCookies.LoginCookie]; 
    if (LoginCookie != undefined) { 
     res.cookie(req.ApplicationCookies.LoginCookie, null, { expires: new Date(Date.now() - 1000), httpOnly: true }); 
    } 
    res.redirect('/'); 
}); 

için çerez yaratıyor node.js zaman uyumsuz davranış hakkında kullanıcıya

var LoggedinEmployee = { 
    EmployeeID : recordset[0][0].EmployeeId, 
    EmployerID : recordset[0][0].EmployerId, 
    EmployerName : recordset[0][0].EmployerName, 
    AccountName : Employer.AccountName, 
    EmpLevel : recordset[0][0].EmpLevel, 
    EmployeeName : recordset[0][0].EmployeeName, 
    CorpId: recordset[0][0].CorpID, 
    Login: user.Login, 
    Password: user.Password, 
    SupportUser: null 
}; 
iron.seal(LoggedinEmployee, req.Security.CookiePassword, iron.defaults, function (err, sealed) { 
    if (!err) { 
     res.cookie(req.ApplicationCookies.LoginCookie, sealed, { expires: new Date(Date.now() + 432000), httpOnly: true, ephemeral: true }); 
     req.ActionOutput.Status = req.ActionStatus.Success; 
     req.ActionOutput.Message = 'Logged In. Redirecting to dashboard...'; 
     res.send(JSON.stringify(req.ActionOutput)); 
    } else { 
     req.ActionOutput.Status = req.ActionStatus.Error; 
     req.ActionOutput.Message = 'System Error'; 
     res.send(JSON.stringify(req.ActionOutput)); 
    } 
}); 

cevap

1

Oku kaydedilir a. Bu kodda çıktı olarak

setTimeout(function(){ 
    console.log(1); 
},0); 
console.log(2); 

output: 
2 
1 

olacaktır. niye ya? node.js bir dizi "keneler" içinde çalıştığı için, birbiri ardına çalışan bir sıradaki yürütme örnekleridir. Sonraki örnek diğer bitmeden önce çalışamaz. Javascript bir "geri arama" ile karşılaştığı zaman, onu sıraya iter ve bir sonraki ifadeyi yürütür. Bu engelleme değil. setTimeout bir geri arama "kaydeder" ve kontrol akışını hemen sonraki satıra geri gönderir. Bu örnek bittiğinde bir sonraki geri arama çağrılacaktır.

Kodunuzda res.redirect ('/'); Iron.unseal bitmiş ve hemen yanıt vermek için yanıt beklemek için beklemek olmaz. Bundan sonra geri aramalar çağrılacak ve res.cookie bu yanıtın daha önce atılmış bir istisna oluşturulmasına neden olduğunu tespit edecektir. Res.redirect ('/') taşı; res.cookie'den sonra.

router.get('/logoutToMain', mustBe.authorized('supportuser'), function (req, res) { 
    var loginSealed = req.cookies[req.ApplicationCookies.LoginCookie]; 
    if (loginSealed != undefined) { 
     iron.unseal(loginSealed, req.Security.CookiePassword, iron.defaults, function (err, unsealed) { 
      if (!err) { 
       unsealed.SupportUser = null; 
       iron.seal(unsealed, req.Security.CookiePassword, iron.defaults, function (err, sealed) { 
        if (!err) { 
         res.cookie(req.ApplicationCookies.LoginCookie, sealed, { expires: new Date(Date.now() + 432000), httpOnly: true, ephemeral: true }); 
        } 
        res.redirect('/'); 
       }); 
       //res.cookie(req.ApplicationCookies.LoginCookie, unsealed, { expires: new Date(Date.now() - 1000), httpOnly: true }); 
      } else { 
       res.redirect('/'); 
      } 
     }); 
    } else { 
     res.redirect('/'); 
    } 
}); 

kullanın vaat veya kodunuzu iyileştirmek ve geri arama cehennem hayatta bekliyor.

+0

https://www.youtube.com/watch?v=l9orbM1MJNk –

+0

Teşekkürler @Nikhil, nodejs .net, java gibi eşzamansız modda çalışmayı unuttum. Aptal hata kodunda yapıldı: D –

+1

Bu hata, yeni node.js devs tarafından çok kez tekrarlanır :) –

İlgili konular