I am trying to resize and upload multiple files using multer. It's working but the problem is when I am sending the data back it is sent before even the data is processed. So I am getting an empty list. I am new to nodejs tried solutions online but couldn't find the right one for me. Can anyone help me solving this problem? How can I push data into the list before sending the response?
the code is Attached below...
router.post('/ads/images', upload.array('images',5), async(req,res)=>{
console.log(req.files);
var data = []
await req.files.every(async(file)=>{
var imageBuffer = await sharp(file.buffer).png().resize({
width:250,
fit: sharp.fit.cover,
position: sharp.strategy.entropy
}).toBuffer()
var thumbnailBuffer = await sharp(file.buffer).png().resize({
width:150,
height:150,
fit: sharp.fit.cover,
position: sharp.strategy.entropy
}).toBuffer()
console.log({imageBuffer,thumbnailBuffer});
data.push({imageBuffer,thumbnailBuffer})
})
console.log(data);
res.send(data)
},(error,req,res,next)=>{
res.status(400).send({error:error.message})
})