如果我在資料庫中放置了與資訊不同的錯誤憑據,則會出現此錯誤:
` node:internal/process/promises:246 triggerUncaughtException(err, true /* fromPromise */); ^
錯誤 [ERR_HTTP_HEADERS_SENT]:在 ServerResponse.setHeader (node:_http_outgoing:574:11) at ServerResponse.header (C:\ Users\Chadi Seif\node_modules\express\lib\response.js:771:10) 在 ServerResponse.send (C:\Users\Chadi Seif\node_modules\express\lib\response.js:170:12) 在 ServerResponse.json (C:\Users\Chadi Seif\node_modules\express\lib\response.js:267:15) 在 ServerResponse.send (C:\Users\Chadi Seif\node_modules\express\lib\response.js:158:21)在exports.loginAdmin (C:\Users\Chadi Seif\Desktop\testdelete\test2\controllers\admin.js:58:21) at processTicksAndRejections (node:internal/process/task_queues:96:5) { 代碼:'ERR_HTTP_HEADERS_SENT' } [nodemon] 應用程式崩潰 - 在開始之前等待檔案更改... `
這是我的代碼
exports.loginAdmin = async (req, res) => {
try {
const { email, password } = req.body;
if (!(email && password)) {
res.status(400).send({ error: [{ msg: " inputs are required" }] });
}
const userfound = await Admin.findOne({ email: req.body.email });
if (!userfound) {
res.status(400).send({ error: [{ msg: "bad credentials" }] });
}
//Compare passwords :
const compare = await bcrypt.compare(password, userfound.password);
if (compare) {
token = jwt.sign({ id: userfound._id }, process.env.TOKEN_KEY, {
expiresIn: "1days",
});
res.status(201).send({ msg: "Happy to see you", userfound, token });
} else {
res.status(503).send({ error: [{ msg: "bad credentials" }] });
}
} catch (error) {
res.status(400).send({ error: [{ msg: error }] });
}
};```
please i need your help community !
uj5u.com熱心網友回復:
該錯誤意味著該代碼之前的事情已經將一些標題/內容回傳給瀏覽器,并且當您嘗試設定新的狀態代碼時,您會收到此錯誤。
那么,檢查您的請求管道或在瀏覽器中查看已發送的內容?
還
您不應在此處回傳503狀態
res.status(**503**).send({ error: [{ msg: "bad credentials" }] });",
錯字?
uj5u.com熱心網友回復:
拋出此錯誤是因為您的函式中沒有回傳值。只是 putres.status(...).send(...)不會結束您的函式執行。通過這種方式,您的函式會嘗試設定每次我們有的發送回應的標頭res.status(...).send(...)
所以,為了解決這個問題,你應該return在任何時候你想像這樣向客戶端發回回應:
return res.status(400).send({ error: [{ msg: " inputs are required" }] });
你的代碼應該像
exports.loginAdmin = async (req, res) => {
try {
const { email, password } = req.body;
if (!(email && password)) {
return res.status(400).send({ error: [{ msg: " inputs are required" }] });
}
const userfound = await Admin.findOne({ email: req.body.email });
if (!userfound) {
return res.status(400).send({ error: [{ msg: "bad credentials" }] });
}
//Compare passwords :
const compare = await bcrypt.compare(password, userfound.password);
if (compare) {
token = jwt.sign({ id: userfound._id }, process.env.TOKEN_KEY, {
expiresIn: "1days",
});
return res.status(201).send({ msg: "Happy to see you", userfound, token });
} else {
return res.status(503).send({ error: [{ msg: "bad credentials" }] });
}
} catch (error) {
return res.status(400).send({ error: [{ msg: error }] });
}
};`
uj5u.com熱心網友回復:
感謝您的回復,我找到了答案……問題與此處發送的回復有關:
if (!userfound) { res.status(400).send({ error: [{ msg: "bad credentials" }] }); }
因為在捕獲錯誤時第二次發送回應,這會導致服務器崩潰
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/378985.html
