1

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 enter image description here When no logo is passed enter image description here

When both parameters are passed enter image description here I want to get the result only when both the parameters are satisfied.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Does this answer your question? [app.get - is there any difference between res.send vs return res.send](https://stackoverflow.com/questions/43055600/app-get-is-there-any-difference-between-res-send-vs-return-res-send) – Shri Hari L Jul 03 '23 at 05:22
  • The `else` only pertains to the last `if`. – deceze Jul 03 '23 at 05:22

2 Answers2

1

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 ")}`);
});
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
-1

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}`
     });
})