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

132
Views
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 answers
Answer question

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 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!