let newProduct = new ProductModel();
for (let i in req.files) {
if (req.files[i][0].fieldname == 'image1') {
cloudinary.uploader.upload(req.files.image1[0].path, { folder: fd, public_id: 'p1' })
.then(pic1 => {
newProduct.photo.push(pic1.url);
newProduct.updateOne({photo: newProduct.photo})
.then(() => res.redirect('back'))
.catch(err => console.log(err))
})
.catch(err => console.log(err))
}
else if (req.files[i][0].fieldname == 'image2') {
cloudinary.uploader.upload(req.files.image2[0].path, { folder: fd, public_id: 'p2' })
.then(pic2 => {
newProduct.photo.push(pic2.url);
})
.catch(err => console.log(err))
}
}
I have the ProductModel that have photo array attribute. I want to add pic.url from cloudinary. But outside the for loop, the newProduct.photo = []. How can I save the value outside the then ?