Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

167
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda