0
var globalVar = ""; 
async.series([
  function(next){
  db.collection.find({query}}).toArray(function(err, result) {
    /*there is a function which updates the 
    value of globalVar to "someupdatedText"*/
    console.log(globalVar); //someupdatedText  
    next(err);
   })
  }, 
  function(next){
      console.log(globalVar); //undefined  
      next();
  }
]);

What could be the problem? In first function I can access the global variable and update its value but in the second function globalVar returns undefined. Is it about node.js? What do I miss?

Yigit Can
  • 361
  • 1
  • 3
  • 15
  • Is `pathListArr` a global variable as well? – cl3m Mar 17 '16 at 09:04
  • Whatever the problem is, the code in the question doesn't reveal it. You need to construct [a proper test case](http://stackoverflow.com/help/mcve). – Quentin Mar 17 '16 at 09:05
  • 2
    I've tried your code and I don't have any issue. Make sure you don't update the variable somewhere else in your code. – cl3m Mar 17 '16 at 09:12
  • I edited my question. I think async.series is not working well, before updating the globalVar, it tries to print. – Yigit Can Mar 17 '16 at 09:20
  • Your issue (which isn't entirely clear still) is probably either [this one](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) or [this one](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron), or a combination thereof. – James Thorpe Mar 17 '16 at 09:36

0 Answers0