2

I have just started with NodeJs and dealing with asynchronous functions. I tried to do multiple MongoDB calls from a for loop, and I need to wait for all of them to complete before the next step.

I have tried to implement it using async, but it seems like all my variable outside the calls are not accessible. Any idea how to make it work?

    var sample = req.body;  // sample will be an array list of items
    var stringList = "";

    var calls = [];
    for(var i = 0; i < sample.length; i++) {
console.log(sample[].item) // i can print it here
        calls.push(function(callback) {
            db3.table.find({column1:sample[i].item}, function(err, temp){  // i hit an error here, it cannot find sample[i].item...
                if (err)
                    return callback(err);
                stringList = stringList + temp[0].item2;
                callback(null, stringList );
            });
        });
    }

    async.parallel(calls, function(err, result) {
        if (err)
            return console.log(err);

console.log(result); // I am expecting a string of all the item2 returned and concatenated previously

});
3
  • calling DB (any) in loop is not a good idea. mostly all database support bulk operations (insert, update) you may do it by one call. can you explain what do you want with database in loop ? Commented Jan 4, 2018 at 11:48
  • It is mostly because I am not sure how to do a complex query with nodejs and MongoDB... what i want is concatenating the result of several items which its column1 = to a few values that i am passing in from the request body Commented Jan 4, 2018 at 12:03
  • post sample data and elaborate whatever you want Commented Jan 4, 2018 at 12:05

3 Answers 3

1

async parallel callback anyway sends the data to final callback, you can use this feature to merge all sent value.

var sample     = req.body;  // sample will be an array list of items
var stringList = "";
var calls      = [];
for (var i = 0; i < sample.length; i++) {
    console.log(sample[i].item) // i can print it here
    calls.push(function (callback) {
        db3.table.find({column1: sample[i].item}, function (err, temp) {
            // i hit an error here, it cannot find sample[i].item...
            if (err) {
                return callback(err);
            }
            callback(null, temp[0].item2);
        });
    });
}

async.parallel(calls, function (err, result) {
    if (err) {
        return console.log(err);
    }
    stringList = result.join('');
    console.log(stringList); // I am expecting a string of all the item2 returned and concatenated previously
});
Sign up to request clarification or add additional context in comments.

1 Comment

pls revert my edit, i edited by mistake and unable to revert
0

Maybe you can check when all DB queries have finished:

var sample = req.body;  // sample will be an array list of items
var stringList = "";
var counter = 0;
for(var i = 0; i < sample.length; i++) {
    console.log(sample[].item) // i can print it here
        db3.table.find({column1:sample[i].item}, function(err, temp){  // i hit an error here, it cannot find sample[i].item...
            if (err)
                throw err;
            stringList = stringList + temp[0].item2;
            if(++counter >= sample.length) {
                // when here you will have the whole string
                console.log(stringList); 
            }
        });
    });
}

Comments

0

every async operation can be express in term of callback or promise. here are two examples might be helpful

//  sequential execution P.S. use for dependent tasks  
var operations= [1,2,3];
(function loop(index){
    if(index==operations.length) return ;
    setTimeout(function() {
        console.log(`hello ${operations[index]}`);
        loop(++index);
    }, 1000);
})(0)

//  parallel execution P.S. use when independent tasks  
Promise.all(operations.map(val=>{
    return new Promise((resolve, reject) => {
        console.log(`hello ${val}`);
    });
}))
.then(data=>{

})
.catch(err=>{
    console.log(err);
})

read more https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.