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 :)
Khan
  • 5,052
  • 5
  • 22
  • 23
  • 1
    need more info/code sample bud – Denis Tsoi Mar 27 '17 at 09:26
  • i havn't coded anything, i am trying to understand how to handle these multiple arrays with ``async`` – Khan Mar 27 '17 at 09:35
  • You'd use try/catch within your async functions for images, videos etc – Denis Tsoi Mar 27 '17 at 09:36
  • can you share a link how to use it? i am new to node ;( – Khan Mar 27 '17 at 09:39
  • https://medium.com/@mgaafar/6-reasons-why-javascripts-async-await-blows-promises-away-tutorial-c7ec10518dd9#.30ihn3347 – Denis Tsoi Mar 27 '17 at 09:41
  • Another reference within SO http://stackoverflow.com/questions/40884153/try-catch-blocks-with-async-await – Denis Tsoi Mar 27 '17 at 09:42
  • more links http://blog.grossman.io/how-to-write-async-await-without-try-catch-blocks-in-javascript/ https://gist.github.com/spion/8c9d8556697ed61108177164e90fb50d – Denis Tsoi Mar 27 '17 at 09:46
  • For uploading file you will need to use multer module. And to check if any video or photo is provided or not use IF statement construction. And to the database you will have to send only links to your uploaded files – JSEvgeny Mar 27 '17 at 11:27

1 Answers1

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
        }
    })
}
Talha Awan
  • 4,573
  • 4
  • 25
  • 40