0

i have a post with text, images, videos and tags option. Except text rest are optional, now using async how can i handle if tags are empty then upload images and if no image is provided then upload video, and if all are given then how can i upload images, videos and then save in db? following are sample inputs

images: [{"image":"image1"},{"image":"image2"}]
videos: [{"video":"video1"},{"video":"video2"}]
tags: [{"userId":2},{"userId":23} ]
text: having a good day :)
8
  • 1
    need more info/code sample bud Commented Mar 27, 2017 at 9:26
  • i havn't coded anything, i am trying to understand how to handle these multiple arrays with async Commented Mar 27, 2017 at 9:35
  • You'd use try/catch within your async functions for images, videos etc Commented Mar 27, 2017 at 9:36
  • can you share a link how to use it? i am new to node ;( Commented Mar 27, 2017 at 9:39
  • medium.com/@mgaafar/… Commented Mar 27, 2017 at 9:41

1 Answer 1

1

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
        }
    })
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.