Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
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 :)

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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");
      }
});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!