Search code examples
javascriptnode.jsexpressfor-of-loop

Node/Express return error to main function?


I'm in a situation where I have a POST route that calls a function multiple times. I want the request to return an error if the called function returns an error, but I am not sure how to achieve this. See this image:

enter image description here

This is my code:

function POSTcord(lat, lng) {
    axios
    .post(process.env.SOS_POST_URL + process.env.SOS_POST_CODE, {
        batteryLevel: 100,
        longitude: lng,
        latitude: lat
    })
    .then(res => {
        console.log(`statusCode: ${res.status}`)
    })
    .catch(error => {
        console.error(error.message);
    })
}

router.post('/test', async (req, res) => {
    let passedCords = req.body;
    try {
        for (const cord of passedCords) {
            POSTcord(cord.lat, cord.lng);
        }
        res.status(200).json({status:"success", message: "hello!"});
      } catch (err) {
        console.error(err.message);
        res.status(500).send("Server error");
      }
});

I want the route /test to return an error if the function POSTcord catches an error somewhere in the loop. Any ideas on this? I'm thinking I could pass res to POSTcord function, but that didn't work. Thankful for any input :)


Solution

  • You need to return the Promise and make sure the error is thrown/rejected:

    Either do this:

    function POSTcord(lat, lng) {
        return axios // <--------------- THIS IS VERY IMPORTANT
        .post(process.env.SOS_POST_URL + process.env.SOS_POST_CODE, {
            batteryLevel: 100,
            longitude: lng,
            latitude: lat
        })
        .then(res => {
            console.log(`statusCode: ${res.status}`)
        })
        .catch(error => {
            console.error(error.message);
            throw error; // <----------- ALSO DO THIS
        })
    }
    

    Or do this:

    function POSTcord(lat, lng) {
        return axios // <--------------- THIS IS VERY IMPORTANT
        .post(process.env.SOS_POST_URL + process.env.SOS_POST_CODE, {
            batteryLevel: 100,
            longitude: lng,
            latitude: lat
        })
        .then(res => {
            console.log(`statusCode: ${res.status}`)
        })
        // DON'T CATCH THE ERROR!!
    }
    

    Then all you need to do is await to get the error:

    router.post('/test', async (req, res) => {
        let passedCords = req.body;
        try {
            for (const cord of passedCords) {
                await POSTcord(cord.lat, cord.lng); // DO THIS FOR CATCH TO WORK
            }
            res.status(200).json({status:"success", message: "hello!"});
          } catch (err) {
            console.error(err.message);
            res.status(500).send("Server error");
          }
    });
    

    If you want to call POSTcord() in parallel you can await using Promise.all():

    router.post('/test', async (req, res) => {
        let passedCords = req.body;
        try {
            let promises = [];
            for (const cord of passedCords) {
                let p = POSTcord(cord.lat, cord.lng);
                promises.push(p);
            }
    
            await Promise.all(promises); // DO THIS FOR CATCH TO WORK
    
            res.status(200).json({status:"success", message: "hello!"});
          } catch (err) {
            console.error(err.message);
            res.status(500).send("Server error");
          }
    });