0

I have an array of documents with unique _id and I want to insert them to my database. Some of them already in db, and for those I want to update an array property (push in array an item). All of this I need to make asyncronuosly, so after all inserted/updated I want to write response back (with callback) to client than all ok or write an error. After googling on subject I've found this solution with async module I've tried to implement it for my case. Now my code looks like this:

function processUsers(arr, listName, callback) {

    var users = global.db.collection('vkusers');
    var q = async.queue(function(task, cb) {
        console.log('upsert butch');
        users.insert(task.doc, function(err, doc) {
            if (err) {
                users.update({
                    _id : task.doc._id
                }, {
                    $addToSet : {
                        mLists : listName
                    }
                }, function(error, result){ console.log(error); console.log(result); });
            }

        });

    }, arr.length);

    for ( var doc in arr) {
        q.push({
            doc : arr[doc]
        }, function(err) {
            if (err)
                callback(err, null);
        })
    }
    q.drain = function() {
        // this is the queue's callback, called when the queue is empty,
        // i.e. when all your documents have been processed.
        console.log('drain');
        callback(null, { result: "success", upserted: arr.length });
    }

}

Callback has signature callback(error, result), arr - my array of documents. I've tested it and with database everything is OK, i am getting the right result. But callback, and q.drain never fired!

Community
  • 1
  • 1
Dmitry Malugin
  • 882
  • 1
  • 7
  • 25

1 Answers1

0

You need to call async.queue's callback (cb in your code) when your insert/update is complete. Something like this:

var q = async.queue(function(task, cb) {
    console.log('upsert butch');
    users.insert(task.doc, function(err, doc) {
        if (err) {
            users.update({
                _id : task.doc._id
            }, {
                $addToSet : {
                    mLists : listName
                }
            }, function(error, result) { 
                console.log(error); 
                console.log(result); 

                cb(error); // Update finished; call cb and pass in "error" so that it can bubble up if it exists
            });
        } else {
            cb(); // Insert succeeded; call cb
        }
    });

}, arr.length);
Mike S
  • 41,895
  • 11
  • 89
  • 84