I'm a complete beginner in node.js so please do excuse me if my question is foolish.Actually what I'm trying to do is I'm getting a list by asynchronous function from mongodb and trying to print that after I get the complete list.Although I'm getting the list correctly but I don't know why it's printing the result as null.Even I thought it could be an error of Promise
that's why I tried to use sync
module but no change in result .Can anyone please help me to fix this error.
Code:
MongoClient.connect(url,function(err,db){
if(err) throw err;
var arr=[]
getBlocker().then(()=>showList())
Sync(()=>{
getBlocker()
showList()
})
function getBlocker(){
return new Promise(resolve=>{
var blQ={blocked_user:data.tag_search_mail}
db.collection("block_list").find(blQ,{"_id":0}).toArray((err,res)=>{
for(let i=0;i<res.length;i++){
arr.push(res[i]["blocker"])
console.log(res[i]["blocker"])//but here it's printing the result correctly that means I'm getting result
}
})
resolve()
})
}
function showList(){
console.log(arr)//Here it's printing []
}
})