Accessing local variables defined in outer function inside callback

46 views
Skip to first unread message

Ram Mulage

unread,
Jun 20, 2016, 9:22:04 PM6/20/16
to nodejs
Hi All,

Sorry, I posted my previous question bit prematurely and I will delete it after I find it.  I am asking that question again,  can we access variables defined inside outer function inside the callback?


router.get('/', function(req, res)
{
utils.dump("viewer::get - " + __filename);
dbutils.fetchMasterConfig(function(config)
{

                var ip = req.ip;

                someClass.someFunc(par1, par2, function(err, file)
                {
                     console.log("ip = " + ip); // is ip accessbile at this scope?
                   
                )};

         });

});


TIA

Yuntaek LIM

unread,
Jun 21, 2016, 12:06:38 AM6/21/16
to nodejs
Hi Ram,

It is accessible because the callback function has been defined in the context of callback of router.get().

Cheers,

Em Dadu

unread,
Jun 24, 2016, 11:39:34 PM6/24/16
to nodejs
Perfectly valid. The callback will have its own context, however this does not effect the scope.

Ram Mulage

unread,
Jul 15, 2016, 5:14:12 PM7/15/16
to nodejs
Thank you all. I was using very old NodeJS (0.15 I think) and looks like it is some kind of bug in nodejs. I fixed it using a global variable but I guess the latest NodJS may have fixed this issue/
Reply all
Reply to author
Forward
0 new messages