0

Been struggling with this for the past hour, can't seem to figure it out:

app.get('/bk/domains/get', function (req, res) {
   client.keys('*', function (e, d) {
       d.forEach(function (data) {
           client.hgetall(data, function (e, d) {
               res.json({'host': data, 'config': d});
           });
       });
   });
});

Here is example output:

{
  "host": "test.com",
  "config": {
    "url": "http://test.com/test.html",
    "token": "source",
    "account": "test",
    "source": "google"
  }
}

Right now, because of res.json it dies right after spitting out the first value from client.hgetall. There are about 10 other values that I would like to add to this json response, but can't seem to get it working. How do I go about doing this?

I tried this:

app.get('/bk/domains/get', function (req, res) {
   var arr = [];
   client.keys('*', function (e, d) {
       d.forEach(function (data) {
           client.hgetall(data, function (e, d) {
               arr.push({'host': data, 'config': d});
           });
       });
   });
});

But arr is always empty. I also tried to move arr outside of app.get as it may have been the namespace, but that doesn't work either.

How can I push the 10 values from hgetall into a single array and return it as json?

Thank you in advance!

  • the hgetall method is asynchronous, arr will only be added to after the get() method is complete – actual_kangaroo Dec 05 '13 at 02:32
  • checkout this answer, you need to wait for all of the arr.push methods ot be called http://stackoverflow.com/questions/11275999/how-do-i-execute-a-function-after-the-callbacks-inside-a-for-loop-are-completed – actual_kangaroo Dec 05 '13 at 02:34

2 Answers2

1

You are probably running into issues due to the asynchronous nature of the client methods. Try this:

app.get('/bk/domains/get', function (req, res) {
   var arr = [], i = 0, numKeys;
   client.keys('*', function (e, d) {
        numKeys = d.length;
       d.forEach(function (data) {
           client.hgetall(data, function (e, d) {
               arr.push({'host': data, 'config': d});
               i++;
               console.log(arr);
               if(i == numKeys)
                res.json(arr);
           });
       });
   });
});
ragamufin
  • 4,113
  • 30
  • 32
0

This is from the nodejs site:

var options = {
  hostname: 'www.google.com',
  port: 80,
  path: '/upload',
  method: 'POST'
};

var req = http.request(options, function(res) {
  console.log('STATUS: ' + res.statusCode);
  console.log('HEADERS: ' + JSON.stringify(res.headers));
  res.setEncoding('utf8');
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

// write data to request body
req.write('data\n');
req.write('data\n');
req.end();

Basically, as one user suggested, it is an asynch call and you must wait to retrieve the data, and then have it write the complete data at the end so you are not left with anything cut-off. For further documentation please review the docs.

C. S.
  • 805
  • 6
  • 6