Monday, 29 May 2017

Array empty outside the Mongoose function

I have a function which fetch data from mongoDB and in each iterator array should be updated with the document results.after the loop, concat array should be send as a callback.in first console log, array is filled with documents as i expected, but outside the async.map it is empty array. i have used async module instead of for loop.but still it's console log the empty array as the result.could anyone point out the mistake in my code.

    module.exports.getFollowersPosts = (usersArray,callbacl)=>{

    var results = [];

    async.map(usersArray,function (item,callback) {

          Blogtab.find({userid:item})
                .sort({'dateAdded': -1})
                 .exec(function(err,docs){

            if(err) {
                callback(err);
            }
            if (docs){

                 results = results.concat(docs);

                console.log(results)
            }

        });

    });
    console.log(results)
    callbacl(null,results)
}



via Rajith

No comments:

Post a Comment