英文:
Why am I not able to check for two parameters in post request?
问题
我正在进行一次POST请求,想要检查是否有任何参数未留空。目前只对一个参数进行了检查,而没有对两者都进行检查。
我希望只在两个参数都满足条件时才获得结果。
英文:
I am making a post request and I want to check if any parameter is not left empty. It is being checked for one parameter but not for both.
app.post("/tshirt/:id", (req, res) => {
const {id} = req.params;
const {logo, size} = req.body;
if(logo === undefined) {
res.status(418).send("It needs a logo");
}
if(size === undefined) {
res.status(418).send("It needs a size");
}
else {
res.status(200).send({
tshirt: `👕 with your ${logo} is ready`,
size: `${size}`
});
}
})
When no size is passed
When no logo is passed
When both parameters are passed
I want to get the result only when both the parameters are satisfied.
答案1
得分: 1
I would return an "error" response if either one or both properties aren't set, with an appropriate message for each case.
So, in the code below, the "error" message can either be:
- It needs a logo
- It needs a size
- It needs a logo and a size
app.post("/tshirt/:id", (req, res) => {
const { id } = req.params;
const { logo, size } = req.body;
if (logo === undefined || size === undefined) {
const msg = [];
if (logo === undefined) {
msg.push("logo");
}
if (size === undefined) {
msg.push("size");
}
// use return here so no `else` is required
return res.status(418).send(`It needs a ${msg.join(" and a ")}`);
}
res.status(200).send({
tshirt: `👕 with your ${logo} is ready`,
size: `${size}`
});
});
Though, to reduce the nesting of if
, I'd probably do it like this (though, many people prefer to handle error cases first):
app.post("/tshirt/:id", (req, res) => {
const { id } = req.params;
const { logo, size } = req.body;
if (logo !== undefined && size !== undefined) {
// return here so no `else` is required
return res.status(200).send({
tshirt: `👕 with your ${logo} is ready`,
size: `${size}`
});
}
const msg = [];
if (logo === undefined) {
msg.push("logo");
}
if (size === undefined) {
msg.push("size");
}
res.status(418).send(`It needs a ${msg.join(" and a ")}`);
});
英文:
Personally, I would return an "error" response if either one or both properties aren't set, with an appropriate message for each case
So, in the code below, the "error" message can either be
- It needs a logo
- It needs a size
- It needs a logo and a size
.
app.post("/tshirt/:id", (req, res) => {
const {id} = req.params;
const {logo, size} = req.body;
if (logo === undefined || size === undefined) {
const msg=[];
if (logo === undefined) {
msg.push("logo");
}
if (size === undefined) {
msg.push("size");
}
// use return here so no `else` is required
return res.status(418).send(`It needs a ${msg.join(" and a ")}`);
}
res.status(200).send({
tshirt: `👕 with your ${logo} is ready`,
size: `${size}`
});
});
Though, to reduce the nesting of if
, I'd probably do it like this (though, many people prefer to handle error cases first)
app.post("/tshirt/:id", (req, res) => {
const {id} = req.params;
const {logo, size} = req.body;
if (logo !== undefined && size !== undefined) {
// return here so no `else` is required
return res.status(200).send({
tshirt: `👕 with your ${logo} is ready`,
size: `${size}`
});
}
const msg=[];
if (logo === undefined) {
msg.push("logo");
}
if (size === undefined) {
msg.push("size");
}
res.status(418).send(`It needs a ${msg.join(" and a ")}`);
});
答案2
得分: -1
请注意在发送响应时使用 return
。
app.post("/tshirt/:id", (req, res) => {
const {id} = req.params;
const {logo, size} = req.body;
if(logo === undefined) {
return res.status(418).send("需要一个标志");
}
if(size === undefined) {
return res.status(418).send("需要一个尺寸");
}
return res.status(200).send({
tshirt: `👕 带有您的 ${logo} 已准备好`,
size: `${size}`
});
})
英文:
pay attention to use return in sending response.
app.post("/tshirt/:id", (req, res) => {
const {id} = req.params;
const {logo, size} = req.body;
if(logo === undefined) {
return res.status(418).send("It needs a logo");
}
if(size === undefined) {
return res.status(418).send("It needs a size");
}
return res.status(200).send({
tshirt: `👕 with your ${logo} is ready`,
size: `${size}`
});
})
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论