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

134
Vistas
document.save() and res.redirect before function is complete in try/catch block

I have some struggles with a conversion function I'm using. As my Mongodb documents saves before the conversion is complete which ends up with an array being empty, which should include my URLs(is verified in the callback). Everything is working fine, but my problem is that I redirect and save document way before apiInstance.convertDocumentPptxToPng is finished.

try {
 const params = {
  Bucket: 'bucket', 
  Key: req.file.key
 }

await s3.getObject(params, async function(err, data) {
 if (err) console.log(err, err.stack); // an error occurred
 else {
  const inputFile = await Buffer.from(data.Body)
  apiInstance.convertDocumentPptxToPng(inputFile, callback)
 }

await course.save()
res.redirect('/course/admin')

})} catch(err) {
console.log(err)
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You are awaiting s3.getObject, but you are also giving it a callback function. Remove the callback function and just use await. course is undefined, not sure it's normal though.

try {
    const params = {
        Bucket: 'bucket',
        Key: req.file.key
    }

    const data = await new Promise( (resolve, reject) => s3.getObject(params,(err, data) => resolve(data)));

    const inputFile = await Buffer.from(data.Body);
    apiInstance.convertDocumentPptxToPng(inputFile, callback);
    await course.save();
    res.redirect('/course/admin');
    
} catch (err) {
    console.log(err)
}
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