skip to Main Content

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.

2

Answers


  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}`
         });
    })
    
    Login or Signup to reply.
  2. 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 ")}`);
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search