Monday, 10 April 2017

How to get variables outside for loop in Nodejs

I am trying to access the variables "onlinehub" and "offlinehub" outside for loop, but the variables prints only the initial declaration values. Is there any other way to do it?

app.get('/test', function(req, res) {
  client.keys('hub*', function (err, keys) {
    console.log(keys.length);
    for (var i=0; i<keys.length; i++){
    client.hgetall(keys[i], function(err, reply) {
    if(reply.status == "online"){
      onlinehub++;
    }
    else if(reply.status == "offline"){
      offlinehub++;
    }
  });
  }
    // for (var i=0; i<reply.length; i++){
    //   console.log(reply.id)
    // }
    console.log(onlinehub +"-" +offlinehub)

    });
});



via Karthikeyan Prakash

No comments:

Post a Comment