I’m trying to extract the mediaLink
and name
properties from each uploaded file to firebase storage, however, what happen is that the files are uploaded successfully but the response is an empty object. What I want is to respond with a collection of objects containing each file properties.
// upload multiple files
router.post('/bucket/uploadfiles/many', upload.any(), async (req, res) => {
try {
const files = req.files;
// check if files is empty
if (!files || files.length === 0) {
return res.send('No file sent.');
}
// collecting promises returned by upload() method
const uploadedPromises = files.map((file) => {
return bucket.upload(file.path, { public: true }).then((data) => {
// extracting desired properties from metadata
const uploadedFile = data[0];
return {
name: uploadedFile.metadata.name,
downloadUrl: uploadedFile.metadata.mediaLink,
};
});
});
const uploadedFiles = await Promise.all(uploadedPromises);
console.log(uploadedFiles); // I get the objects as expected
res.send(uploadedFiles); // this is sent before the promise is resolved thus an empty object
} catch (error) {
res.send(error);
}
});
Could someone please help me to understand what I’m missing here.