0

I am trying to periodically load some data from external API like this:

setInterval(function() {
  getData();
}, 60000);

function getData() {
  if (typeof someObject.data === 'object') {
    for (var prop in someObject.data) {
      if (prop === 1 || prop === 2) {
        var options = {
          host: 'somehost.com',
          path: '/somepath?param=' + prop
        };
        var req = http.request(options, function(res) {
          // EXECUTION NEVER REACHES THIS POINT ?!?!
          req.on('end', function() { alert('ended'); });
        });
        req.end();
      }
    }
  }
}

If I do not do any intervals and loops, such request to the same host works perfectly. However, if I try to do something like shown above, then the request never ever calls its callback function.

What I am doing wrong here?

2
  • Maybe your variables (someobject) is not in scope (undefined ),have you tried debugging it line by line, and see if everything is defined? Commented Apr 24, 2013 at 7:20
  • In fact, I have a check typeof someObject === 'object' as well, I just didn't include it here for better readability. It reaches the line where the request is made but it never returns. If I remove the other stuff (setInterval and the loop), everything executes perfectly. Commented Apr 24, 2013 at 7:23

1 Answer 1

1

I think one of your conditions are bad, the following works fine for me.

var http = require('http');

setInterval(function() {
  getData();
}, 1000);

function getData() {
  console.log('get');
  //if (typeof someObject.data === 'object') {
    console.log('get 1');
    //for (var prop in someObject.data) {
      console.log('get 2');
      //if (prop === 1 || prop === 2) {
        console.log('get 3');
        var options = {
          host: 'google.com',
          path: '/'
        };
        var req = http.request(options, function(res) {
          console.log('http request', res.statusCode);
          //req.on('end', function() { 
          //  console.log('ended', req); 
          //});
        });
        req.end();
      //}
    //}
  //}
}

also If I'm right, you don't need req.on('end'), the callback of the request is called when it's completed. You can also use http.get, so you don't need to call req.end

    var req = http.get( options.host, function(res) {
      console.log('http request', res.statusCode);
      //req.on('end', function() { 
      //  console.log('ended', req); 
      //});
    }).on('error', function( e ) {
      console.error( 'error', e );
    }) 

see more info in the docs

hope I could help.

Sign up to request clarification or add additional context in comments.

2 Comments

Yep, seems to be related to the environment / other parts of code. Isolated test runs well. Thanks.
You can simplify your code with some modules. Take a look at async github.com/caolan/async and request github.com/mikeal/request

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.