javascript - in nodejs access variable outside callback like gloabal varaible -


this question has answer here:

    var coll= '';     function test(callback){         mongoclient.connect(url, function(err, db) {           if(err) throw err;           coll=db           callback(coll);         });     }      test();     console.log(coll) 

this code. when print 'coll' variable says undefined. how access variable outside of callback. getting error callback not function

you not passing callback function argument test() function, therefore statement in test() function: callback(coll), throw error ending script prior calling console.log(coll).


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

Laravel mail error `Swift_TransportException in StreamBuffer.php line 269: Connection could not be established with host smtp.gmail.com [ #0]` -

c# SetCompatibleTextRenderingDefault must be called before the first -