3
I have a code block where an array type variable is defined and within a function map
, I push some values to that array. Inside the map
, I can print the array with the filled objects. However, outside the map
the array is empty. I am referring to the variable resultImage
.
My code:
async store(req, res) {
const schema = Yup.object().shape({
title: Yup.string().required(),
price: Yup.number().required(),
negotiable: Yup.boolean().required(),
status: Yup.boolean(),
description: Yup.string().required(),
});
if (!(await schema.isValid(req.body))) {
return res.status(400).json({ error: "Validate fails" });
}
const { id: idAd, title, price, negotiable, description } = await Ad.create(req.body);
const imgs = req.files;
let resultImg = [];
imgs.map(async item => {
const { id: idFile, url } = await File.create({ ad_id: idAd, url: item.path });
resultImg.push({ id: idFile, url });
})
console.log(resultImg);
return res.status(201)
.json({ idAd, title, price, negotiable, description, imag: { files: resultImg } });
}
Very good explanation. I get it. Thank you.
– user145848