Use async parallel
for your optional videos and images uploading. Inside parallel if your service does not allow multiple insertions then you need to use async
each
or map
.
const async = require("async");
// example data
const data = {
images: [{"image":"image1"},{"image":"image2"}]
videos: [{"video":"video1"},{"video":"video2"}]
tags: [{"userId":2},{"userId":23} ]
text: "having a good day :)"
}
if(!data.text){
//return 'text is required' message
}
else{
const imageUrls = [];
const videoUrls = [];
// http://caolan.github.io/async/docs.html#parallel
async.parallel({
uploadImages: function(callback){
async.each(data.images, function(image, eachCallback){
// upload each image
// push returned url in imageUrls
// call return eachCallback(), or if err then return eachCallback(err)
}, function(err){
if(err){
return callback(err)
}
else{
return callback()
}
})
},
uploadVideos: function(callback){
async.each(data.videos, function(video, eachCallback){
// same process as images
}, function(err){
if(err){
return callback(err)
}
else{
return callback()
}
})
}
}, function(err, results){ //final callback of async parallel
if(err){
//handle err
}
else{
const obj = {
images: imageUrls,
videos: videoUrls,
tags: data.tags,
text: data.text
}
//insert obj to db and respond with success message
}
})
}