nodejs async.whilst will only invoke once

216 Views Asked by At

As the question states, async.whilst will only invoke once. However, if I pass an array containing 2 objects (length of 2), it will only invoke once -- instead of once for each index of the array.

//if previous awsKeys detected in req.body / images are detected : delete them.
    exports.multipleDelete = function (req, res, next) {
        var body = req.body;
      //if object already has existing keys, they will be in the body.existingKeys array.
        var awsKeyTrash = body.existingKeys;
        if (awsKeyTrash !== undefined) {
            var j = 0;
            async.whilst(
                function () {
                    return j < awsKeyTrash.length;
                },
                function () {
                    var picInfo = awsKeyTrash[j];
                    s3.deleteObject({
                        Bucket: aws.bucket,
                        Key: picInfo.key
                    }, function (err, data) {
                        if (err) {
                            console.log('delete err', err);
                    //if there is an error, set pic information to original
                            req.body[picInfo.name] = picInfo.picUrl;
                            req.body[picInfo.key] = picInfo.awsKeyVal;
                            j++;
                        };
                        console.log('deleted')
                        console.log('j ', j)
                        j++;
                        res.send('deleted');
                    });
                },
                function (err) {
            console.log('profile edit , pic delteion err : ', err);
            return res.status(409).send({
              message: ['Unable to edit profile at this time. Try again']
            });
                })
            next();
        }
        else {
            next();
        }
    }

here is an example of of the body.existingKeys array:

     Array[
        {
        awsKeyVal: "users/66085aa8-6501-4f90-973c-1b18edf4087eScreenShot2016-12-05at10.03.07PM.png",
        key: "awsPictureKey",
        name: "picture",
        picUrl: "https://example-bucket.s3.amazonaws.com/users/66085aa8-6501-4f90-973c-1b18edf4087eScreenShot2016-12-05at10.03.07PM.png"
    }, 
        {
        awsKeyVal: "coverphoto/7180d1ae-748c-4b96-86cb-5cb29cebdc9bScreenShot2016-12-10at3.13.18PM.png",
        key: "awsCoverKey",
        name: "cover",
        picUrl: "https://example-bucket.s3.amazonaws.com/coverphoto/7180d1ae-748c-4b96-86cb-5cb29cebdc9bScreenShot2016-12-10at3.13.18PM.png"
    }]
1

There are 1 best solutions below

2
On BEST ANSWER

async.whylist takes a callback that you aren't calling.

It looks like your next() is misplaced as well.

Additionally, the last function is not an error handler. It just takes the error as the first argument, which may be null if there is no error.

async.whilst(
    function () {
        return j < awsKeyTrash.length;
    },
    function ( callback ) {
        var picInfo = awsKeyTrash[j];
        s3.deleteObject({
            Bucket: aws.bucket,
            Key: picInfo.key
        }, function (err, data) {
            if (err) {
                console.log('delete err', err);
                //if there is an error, set pic information to original
                req.body[picInfo.name] = picInfo.picUrl;
                req.body[picInfo.key] = picInfo.awsKeyVal;
                j++;
            };
            console.log('deleted')
            console.log('j ', j)
            j++;
            res.send('deleted');

            callback();
        });
    },
    function (err) {

        if( err ) {
            console.log('profile edit , pic delteion err : ', err);
            return res.status(409).send({
                message: ['Unable to edit profile at this time. Try again']
            });
        }

        // If someone else handles errors, pass it on here. Otherwise,
        // throw or something else to block program flow
        next(err);
    }
);