![](/img/trans.png)
[英]Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client in Node JS
[英]Node JS error: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
我正在嘗試在我的應用程序中實施身份驗證。 但是,應用在注冊用戶時拋出 cannot set header 錯誤。 我無法理解為什么通過錯誤處理 function“wrapAsync”會拋出此錯誤。
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (node:internal/errors:387:5)
at ServerResponse.setHeader (node:_http_outgoing:603:11)
at ServerResponse.header (/Users/alisha/Desktop/Oshi/node_modules/express/lib/response.js:794:10)
at ServerResponse.send (/Users/alisha/Desktop/Oshi/node_modules/express/lib/response.js:174:12)
at done (/Users/alisha/Desktop/Oshi/node_modules/express/lib/response.js:1035:10)
at /Users/alisha/Desktop/Oshi/node_modules/ejs-mate/lib/index.js:356:7
at tryHandleCache (/Users/alisha/Desktop/Oshi/node_modules/ejs/lib/ejs.js:280:5)
at Object.exports.renderFile (/Users/alisha/Desktop/Oshi/node_modules/ejs/lib/ejs.js:491:10)
at View.renderFile [as engine] (/Users/alisha/Desktop/Oshi/node_modules/ejs-mate/lib/index.js:298:7)
at View.render (/Users/alisha/Desktop/Oshi/node_modules/express/lib/view.js:135:8)
這是我的代碼塊:
1. /注冊路線:
router.post('/register', wrapAsync(async(req,res)=>{
const {username, email, nearestLocation, phoneNum, password, RePassword}= req.body;
if(password !== RePassword) {
req.flash('error', 'Please retype same password for confirmation')
return res.redirect('/firm/register')
}
const hashpw= await bcrypt.hash(password, 12);
const registerBusiness= new Businesses({
username: username, email: email, location: nearestLocation, phoneNum: phoneNum, password: hashpw})
await registerBusiness.save();
req.session.currentAccount= registerBusiness.id;
req.session.currentAccountName = registerBusiness.username;
req.flash('success', 'Successfully registered your Business account')
return res.redirect('/home')
}))
2. wrapAsync模塊:
module.exports = (fn)=>{
return ((req,res,next)=>{
fn(req,res,next).catch(next)
})
}
誰能向我解釋這個錯誤。
我期待這段代碼為請求發送一個響應。 但是,情況似乎並非如此,我也無法確定第二個響應是如何發送的。 幫我找答案...
這是我這邊的一個非常簡單的錯誤。 我現在想通了這個問題。 router.get 和 router.post 路由都在發送對相同 /register 請求的響應。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.