Hi I am look for a solution for API server side upload using Node Js asyc wait functionality for uploading multiple images then catch into an array. I follow this link to implement my code. In console it shows the array that I expect, but it does not give me any responses.
Here is what I tried so far,
exports.upload_image = async(req, res) =>{
//let filePaths = req.files.path;
let multipleUpload = new Promise(async (resolve, reject) => {
let upload_len = req.files.length;
let upload_res = new Array();
for(let i = 0; i < upload_len; i++)
{
let filePath = req.files[i].path;
await cloudinary.v2.uploader.upload(filePath, { use_filename: true, unique_filename: false }, function (error, result) {
if(upload_res.length === upload_len)
{
/* resolve promise after upload is complete */
resolve(upload_res)
}
else if(result)
{
/*push public_ids in an array */
upload_res.push(result.public_id);
} else if(error) {
console.log(error)
reject(error)
}
})
}
})
.then((result) => result)
.catch((error) => error)
let upload = await multipleUpload;
res.json({'response':upload})
}
Any suggestion would be appreciated. Thanks.
await multipleUpload();awaitingcloudinary.v2.uploader.upload, but I don't that returns a promise.awaitonly works on functions that return a promise, not functions that just take an ordinary callback. You will have to "promisify" that function (convert callback to a promise) to use it withawait.if(upload_res.length === upload_len)line of code I modified asif(upload_res.length == req.files.length-1)then it gave me the response, but it is only having details for two images while I send three images on postman. Here is the response{ "response": [ "1529884523808bebbb1d5ff29328157080e4e2e5d15a29c5aa34a", "1529884523809d2bf46fbe4c12e06481303d85f49f125e18fbd39" ] }