0

How can I pass the result of my MongoDB query to a const variable so it can be used globally across my application

Here is what i have tried

let dimension;

Settings.find({_id : "5d7f77d620cf10054ded50bb"}).select('dimension').exec((err , found) =>{
   if(err){
      console.log(err);
   }else{
     dimension = found[0].dimension;
     console.log(found[0].dimension); ///here actually print out my expected result....
   }
});


console.log(dimension) /// Here prints undefined
phemieny7
  • 803
  • 7
  • 21
  • 1
    Even though the dup target mentions ajax and http requests, the problem is the same here: you can't return value from an async operation like that. Your `console.log(dimension)` is executed __before__ your query success callback (callback will be executed "sometime later", because the query is async) – Sergio Tulentsev Sep 16 '19 at 20:23
  • @SergioTulentsev then can you help me do it – phemieny7 Sep 17 '19 at 11:04

0 Answers0