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)
}
CodePudding user response:
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)
}
CodePudding user response:
You can await like this.
await Promise.all(apiInstance.convertDocumentPptxToPng(inputFile, callback));