I am new to Javascript. I'm trying to make a loop that uploads the images to Google Cloud Storage. With this code, the image is uploaded correctly. The problem is that the path (URL) is not saved in the database AFTER the upload.
I tried using async and await. However, I do not understand how it works. I want the for-loop and everything inside to be finished before saving post.
Thank you in advance,
for (const [i, file] of files.entries()) {
newFileName = "img_" + Date.now() + "_" + file.originalname;
imagePath = getPublicUrl(newFileName);
myfile = bucket.file(newFileName);
stream = myfile.createWriteStream({
metadata: {contentType: file.mimetype},
resumable: false
});
sharp(file.buffer)
.resize({ width: 1080 })
.pipe(stream)
.on('finish', () => {
post.images.push(imagePath);
})
}
post.save();