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 On Fri, Jun 17, 2016, 7:28 AM Ram Mulage <themul...@gmail.com> wrote: > 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 > > > -- > 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+unsubscr...@googlegroups.com. > To post to this group, send email to nodejs@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/nodejs/2154584f-db49-48dc-a07b-e1378fd27f07%40googlegroups.com > <https://groups.google.com/d/msgid/nodejs/2154584f-db49-48dc-a07b-e1378fd27f07%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- ~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+unsubscr...@googlegroups.com. To post to this group, send email to nodejs@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/CAGjmZGwNPXOC2DFYqmL-zFEzEay%3DN7OSDkmhAjVCr-q75V29tQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.