我試圖用一個空的正文在郵遞員中測驗我的登錄請求,但這個檢查不起作用。它超越并執行其余的代碼。為什么 ?有沒有另一種方法來檢查 body 是否為空?
route.post("/login", (req, res) => {
console.log(req.body);
if (!req.body) {
console.log("I am here");
res.status(400).send({ message: "Content cannot be empty" });
return;
}
... // check password etc
}
uj5u.com熱心網友回復:
您可以檢查 req.body 有多少鍵
if (Object.keys(req.body).length === 0) {
console.log("I am here");
res.status(400).send({ message: "Content cannot be empty" });
return;
}
uj5u.com熱心網友回復:
您可以檢查 Body 的長度:if (Objects.keys(req.body).length === 0) {
uj5u.com熱心網友回復:
您可以使用:
if(Object.keys(req.body).length === 0)
或者
Object.getOwnPropertyNames(req.body).length == 0
然后添加您的邏輯。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/460492.html
標籤:javascript 节点.js 表示 邮政 邮差
