Something strange going on in Async callback

46 views
Skip to first unread message

Ram Mulage

unread,
Jun 17, 2016, 10:29:06 AM6/17/16
to nodejs
Hi All,

I am fairly new to the Node.JS and Javascript for that matter. Something strange going on in the below code

The below page/code is requested by two different clients. Its all good for the first request as it logs correct IP of the requested client correctly every where.
but for the second request, the IP address(reqHostIp) is correct outside the CLOSURE but inside the CLOSURE BLOCK the value of the IP address is always of the first request's 

I am suspecting this is to do with variable scoping or something of that sort or is it something else? Do you find anything obviously wrong in the code


router.get('/', function(req, res)
{
utils.dump("viewer::get - " + __filename);
//var reqHostIp = req.connection.remoteAddress || req.headers['x-forwarded-for'];
dbutils.fetchSomeDetals(function(config)
{
//Some code
//Some code
res.render('viewer',
{
//Rendering code
});
//let reqHostIp = req.ip;
var reqHostIp = req.connection.remoteAddress || req.headers['x-forwarded-for'];
utils.dump("reqHostIp: " + reqHostIp); //Correct IP logged
//***********CLOSURE***********

(function(reqHostIp) 
{
someClass.someFunction(id, ip, metric, function(err, file_name)
{
if (err)
{
utils.dump("viewer::get - comms error: " + err + ", failed");
return;
}
let src = '/tmp/' + file_name;
utils.dump("reqHostIp: " + reqHostIp); // Wrong! Always displays the previous request's IP 
});
})( reqHostIp);

//**************END CLOSURE*******************

utils.dump("reqHostIp: " + reqHostIp); // Correct IP logged 
});
});


TIA


Ryan Graham

unread,
Jun 17, 2016, 4:30:36 PM6/17/16
to nodejs

I don't see anything obvious, but these are the questions I would be asking myself while investigating.

Is "someClass.someFunction" taking a single use call callback? Or does the first call to it register the callback somewhere and then reuse it for future results?

Your extra closure wrapping doesn't appear to be necessary, but it shouldn't be causing any problems. Does the behavior change if you remove it?

Are you using a transpiler, or is this code run as-is?

~Ryan


--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/2154584f-db49-48dc-a07b-e1378fd27f07%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--

~Ryan

Ram Mulage

unread,
Jun 20, 2016, 9:22:08 PM6/20/16
to nodejs
And it is running in a live environment and so no transpiler.

Ram Mulage

unread,
Jun 20, 2016, 9:22:10 PM6/20/16
to nodejs
Thanks for the reply, Ryan. I don't think the code is doing registering of callbacks but I will check again.  You are right, the extra wrapping of the Closure doesn't affect the behavior in any way, I just had it to avoid any issues related to scoping of the variables. If I remove the Closure the behavior is still the same. 


On Friday, June 17, 2016 at 9:30:36 PM UTC+1, Ryan Graham wrote:

Ram Mulage

unread,
Jul 15, 2016, 5:14:00 PM7/15/16
to nodejs
I am using very old version(0.15) of NodeJS and this behavior is due some kind of bug in the NodeJS and I guess the latest versions may not suffer from this. I fixed it by making the reqHostIp  a global variable. But I have to upgrade the NodeJS at some point. Thanks again
Reply all
Reply to author
Forward
0 new messages