У меня есть node.js
сервер. И мне надо хранить файлы по указанному c API. Моя функция для него:
exports.bootcampPhotoUpload = asyncHandler(async (req, res, next) => {
...
const fileNames = [];
files.forEach((item) => {
item.name = `photo_${bootcamp._id}${path.parse(item.name).ext}`;
item.mv(`${process.env.FILE_UPLOAD_PATH}/${item.name}`, async err => {
console.log(item.name); // logs on console second and shows fileName
fileNames.push(item.name);
});
});
console.log(fileNames); // logs on console firstly and shows []
const updated = await Bootcamp.findByIdAndUpdate(req.params.id, { photo: fileNames });
console.log(fileNames); // logs on console third and shows [filenames]
res.status(200).json({
success: true,
data: fileNames
});
});
И на моей модели сохранено:
{
photo: []
}
item
объект:
{ name: 'photo_2020-04-18 14.08.56.jpeg',
data:
<Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 01 00 48 00 48 00 00 ff db 00 43 00 04 03 03 04 03 03 04 04 03 04 05 04 04 05 06 0a 07 06 06 06 06 0d 09 0a 08 ... >,
size: 208920,
encoding: '7bit',
tempFilePath: '',
truncated: false,
mimetype: 'image/jpeg',
md5: 'f8714b4e2135c3873c939a9464ea380a',
mv: [Function: mv]
}
Для загрузки файла я использую express-fileupload
. Существует проблема с выполнением async/await
. Как я могу сохранить его правильно?