为什么我不能在POST请求中检查两个参数?

huangapple go评论83阅读模式
英文:

Why am I not able to check for two parameters in post request?

问题

我正在进行一次POST请求,想要检查是否有任何参数未留空。目前只对一个参数进行了检查,而没有对两者都进行检查。

当未传递尺寸参数时:
为什么我不能在POST请求中检查两个参数?

当未传递标志参数时:
为什么我不能在POST请求中检查两个参数?

当两个参数都传递时:
为什么我不能在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
为什么我不能在POST请求中检查两个参数?
When no logo is passed
为什么我不能在POST请求中检查两个参数?

When both parameters are passed
为什么我不能在POST请求中检查两个参数?
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}`
     });
})

huangapple
  • 本文由 发表于 2023年7月3日 13:17:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76601994.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定