Problem uploading multiple images in node.js/Mongodb with Multer and Cloudinary
Hi all. I'm having an issue trying to upload multiple images to my page. I'm using Multer and Cloudinary. Since Cloudinary doesn't have a method for multiple uploads, I passed the array of images (previously extracted with Multer) through a forEach. Ok, the images are successfully uploaded to mi cloudinary container. The problem comes into the upload function, when I trie to push the secure url and the public id into req.body.product. If I console.log(req.body.product) outside the upload function, it returns name and price (data from the form) but nothing about the arrays of images an ids. But if I console.log it inside de upload function, it returns the complet object (name, price, and both arrays). Product.create is taking the object without the arrays so I get my images uploaded to Cloudinary but I can't push the urls into my database to use in the showpage. It seems to me there's a scope issue I'm overlooking, but I can't figure out. Sorry for the long text, I want to be specific enough. Multer and cloudinary are correctly configured, I'm being able to upload single images in another routes.
Thanks in advance

-
Hi Francisco,
Cloudinary uploader is asynchronous and it might not have finished executing when you are printing the console.log outside the scope of the uploader but it eventually executes and returns the result inside the uploader. Here is an example of implementing it using a Promise and then() call: https://stackoverflow.com/questions/43701013/cloudinary-api-resolve-promise
Hope this helps.
Regards,
Aditi
0
Post is closed for comments.
Comments
1 comment