Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have been trying to get this working

var http = require('http'),
    parseString = require ('xml2js').parseString;

for (var noOfZones = 1; noOfZones<=8; noOfZones++) {
    var newPath = 'http://192.168.1.200/getZoneData?zone='+noOfZones;
    var req = http.request(newPath, function(zres, noOfZones) {
        zres.setEncoding('utf8');
        zres.on('data', function (zonedata) {
            parseString(zonedata, {
                trim:true,
                childkey:9,
                explicitArray:false,
                explicitChildren:false
            }, function (err, zones) { 
                   console.log(noOfZones);//this one bring back undefined
            });
         });
     });
     req.on('error', function(e) {
        console.log('problem with request: ' + e.message)
     })
     req.end();
}

The for loop works ok for passing the noOfZones to newPath. However the value is not being passed through within the function (err, zones) I have been reading on passing the variables to inline functions and attempted to place it at various levels but it doesn't seem to be working for me. All I get back is undefined. (Even if i stringify it)

share|improve this question
    
Google for "infamous loop problem javascript" –  Yury Tarabanko Jul 11 at 12:04

2 Answers 2

up vote 0 down vote accepted

What makes you think request will pass additional arguments to your callback?

You can do the following:

var http = require('http'),
    parseString = require ('xml2js').parseString;

Array.apply(null, new Array(8)).forEach(function(_, i) { //make iterable array
    var noOfZones = i + 1; //define scope variable
    var newPath = 'http://192.168.1.200/getZoneData?zone='+noOfZones;
    var req = http.request(newPath, function(zres) {
        zres.setEncoding('utf8');
        zres.on('data', function (zonedata) {
            parseString(zonedata, {
                trim:true,
                childkey:9,
                explicitArray:false,
                explicitChildren:false
            }, function (err, zones) { 
                   console.log(noOfZones); //access it
            });
         });
     });
     req.on('error', function(e) {
        console.log('problem with request: ' + e.message)
     })
     req.end();
});
share|improve this answer
    
Thanks. I am just picking up javascript. That works! Will go research iterable arrays. –  IanJ Jul 11 at 12:24
    
    
Thanks for that. –  IanJ Jul 13 at 3:53

Should work if you get rid of the "var" before noOfZones in the loop declaration. So for (noOfZones = 1; noOfZones<=8; noOfZones++) instead of for (var noOfZones = 1; noOfZones<=8; noOfZones++).

share|improve this answer
    
Thanks...seems to bring back something. However it's now my 'to' value +1 (9) –  IanJ Jul 11 at 12:13
    
Hmm -1 for promoting globals. And this wont help all functions will share the same value of noOfZones. –  Yury Tarabanko Jul 11 at 12:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.