0

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 []
            }
        })
Srinivas Nahak
  • 1,846
  • 4
  • 20
  • 45
  • Asynchronous results CANNOT be returned synchronously (which is what you are trying to do). It's a matter of timing. Your function returns BEFORE the asynchronous results are even available, thus you see an empty array. – jfriend00 Mar 31 '18 at 15:36
  • Also, `if(err) throw err;` inside an asynchronous callback is NEVER the right code. You need to write real error handling, communicating an error results back to the caller via a promise or callback. – jfriend00 Mar 31 '18 at 15:37
  • @jfriend00 then sir could you please guide me towards the right answer? – Srinivas Nahak Mar 31 '18 at 15:42
  • It's ALL explained in the answer that you're was marked a dup of. This is a commonly asked question and that other answer fully explains the issue. – jfriend00 Mar 31 '18 at 15:43
  • @jfriend00 sir I'm sorry to disturb you again although that dup. answer is explaining everything but as I'm a beginner so I don't know how to implement callback in my case . It would be a great help if you could spare some of your time to write code for my case . – Srinivas Nahak Mar 31 '18 at 16:02
  • First off, you should probably use a promise instead of a plain callback as that is the modern way to do asynchronous things in node.js. Second, there are examples of implementing both callbacks and promises in the answer yours was marked a dup of. If you write some code to follow those examples and get stuck doing that, then you should write a new question and post the code you tried and explain where you got stuck. There's no reason for us to retrace all the ground that has been covered before. – jfriend00 Mar 31 '18 at 16:07
  • @jfriend00 okay thanks – Srinivas Nahak Mar 31 '18 at 16:09
  • @jfriend00 sir could you please check my this [question](https://stackoverflow.com/questions/49590470/implementing-node-js-call-back-to-get-result/49590586?noredirect=1#comment86189107_49590586) – Srinivas Nahak Mar 31 '18 at 17:51

0 Answers0